Binding to ObservableCollection from Custom Control - c#

I have a custom control with an ObservableCollection...
In my Generic.xaml, I have the control template defined there and I would like to bind the ListBox there to the observablecollection of its own custom control (INotifyPropertyChanged implemented), which is different than normal since we're not binding to the view model.
Is this possible?

If you want to bind a control in a control template (listbox in your case) to an object in the custom control (ObservableCollection in your case), you should define the object as a dependency property in the custom control and then use 'TemplateBinding' to bind to the object in the control template.
I am however wondering if its indeed a custom control that you want and not a user control.

Related

Multiple pages in a single window C# WPF

Is there a control that allows to choose data, that should be in it? For example: red one - static page, blue is dynamic, below are buttons that allow to switch pages in dynamic part.
I use the dockpanel suite for this:
dockpanel suite
This contol is designed to mimic the vs.net tabs.
Try using a ContentControl databound to a property on your UserControl. Change the property when you want to update the content. Ensure that the property is either and DependencyProperty or that your user control implements INotifyPropertyChanged.
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx
This is achived by using DataTemplates. You just bind the SelectedItem of the "red" control (e.g. a ListView) to the Content of the "blue" Control (it should be a ContentPresenter).
You add DataTemplates to the Resources accessable by the "blue" control which will then display different content depending on the underlaying class/data.

How to bind a grid to a user control

I want to create a settings menu that looks similar to VLC's advanced settings menu: Treeview on the left and some kind of control collection on the right. The controls on the right should enable the user to manipulate settings that are relevant to the current selection in the tree view. I thought about creating a grid right of the tree view. Then I have a user control for each view that needs to be displayed in the grid, based on the selection
The item in the treeview has a UserControl property that holds a reference to the relevant view. My viewmodel has a SelectedItem property that indicates which item in the tree view is currently selected.
Now I want to bind the content of the grid to the UserControl property of my SelectedItem. But I cannot figure out how to do that. I would prefer to use a XAML based solution instead of clearing the Children property of the grid and adding the user control that I want to display in code each time the SelectedItem property changes.
I'd suggest using ContentControl instead of Grid.
Considering the tree view and the content control are under the same view model: on your view model, add property for selected item (let's call it VMSelectedItem) of same type as the items in the tree view.
In XAML of the tree view add
SelectedItem="{Binding VMSelectedItem}"
In XAML of the content control
Content="{Binding VMSelectedItem.UserControl}"
Now selection in the tree will update the VMSelectedItem property that, in turn, will update the content of the content control.
I suggest you using DataTemplates that you have declared inside your resouces dictionay. You would be using only one instance of each DataTemplate which leaves nice memory footprints. You wouldnt need to store instance of view inside your viewmodel which is the basic idea of mvvm. The viewmodel would completely just holding data and information how you wish the data to be displayed.
As example you have an enum inside your viewmodel with values person, car, tree. Inside your DataTemplateSelector you will have an if on that enum that returns what the desired DataTemplate.
Basically you would have everything central instead of having everything per each TreeViewItem.

Extending TabControl and TabItem

I am just trying out extending TabControl and TabItem for fun; including providing custom styles. I am creating the ExTabControl programatically, and adding several ExTabItem(myDataObject) to the tabcontrol. myDataObject has several properties, like "Title" and "Editor." Editor is of type UIElement.
What I am struggling with is how do I bind the Editor property to be the tab panel's content?
Bind the tab item's content property to a control or another property.

Instantiate ItemsPanelTemplate

I'm trying to create a ItemsControl that has a separator between items, for example a control to create a navigation bread crumb. I want the control to be completely generic.
My original method was to create extend ItemsControl, add a SeparatorTemplate property, and then have the class add separators to the ItemsHost of the ItemsControl. The problem with this approach is that if you add extra items to the container panel, the ItemGenerator gets confused and the items are out of order and don't get removed correctly.
So my second plan was to create a completely new control that would emulate an ItemsControl, but the problem I'm running into is that I can't find a way to instantiate an ItemsPanelTemplate. I would like to provide an ItemsPanel property just like ItemsControl, but I can't then create a panel from that template.
Can anyone think of a way to either instantiate an ItemsPanelTemplate or way to add controls to an ItemsControl's panel without breaking the ItemGenerator?
Well I haven't tried it myself but I would have thought you would override the GetContainerForItemOverride to acheive this.
You could create a new BreadCrumbItem control which is a templated ContentControl that has in its default template the typical ContentPresenter and whatever you want to use by default as separator all in a Grid or StackPanel.
The GetContainerForItemOverride generates a new instance of this BreadCrumbItem sets its ContentTemplate to the ItemTemplate property from your ItemsControl derivative (the BreadCrumb control?).
Your BreadCrumb control would also expose BreadCrumbItemStyle property that you assign to the BreadCrumbItem you create during GetContainerForItemOverride.
For completeness you may need to also implement the other *Container*Override methods in your BreadCrumb control.

Click-to-edit in Silverlight

Is there a way to make a "click-to-edit" control in silverlight? I've got some items that
will be displayed in a treeview control, and I would like the labels to be editable directly in the treeview.
Anyone know how to do this?
Very easy actually. I have implemented many forms with such a swapping mechanism.
You could do this using a Converter and do a simple BooleanToVisibility conversion on an IsEditable property that exists on the entities that you bind to your TreeView. Within your TreeView ItemTemplate just bind the TextBlock in such a way that it is Collapsed whenever the IsEditable property is true and bind the TextBox in such a way that it is collapesed when IsEditable property is false (and vice versa).
If you wanted to build a custom ClickToEdit control you would need to do the following:
Create a class that inherits from ContentControl
Expose a new dependency properties of type DataTemplate: one called EditableTemplate.
Add a MouseLeftButtonUp event handler inside your OnApplyTemplate to listen for the click.
Change the active content template to be your EditableTemplate on the click event.
Change the template back when the control loses focus.
Now to use your custom control inside TreeView:
Override your ItemTemplate for your TreeView
Put your custom ClickToEdit control inside there
Implementing a custom control would allow you (or other developers) to easily specify what control they wanted to use as the content editor. For example, they could specify a NumericUpDown or a DateTimePicker instead of just using a TextBox.
Check out DataForm in Silverlight 3. It has similar functionality but the switching of the editable vs. read-only is not done by a click.

Categories