I have ItemsControl with multi DataTemplate and use ItemTemplateSelector to chose appropriate DataTemplate of each item of ItemsSource. I want to binding ItemTemplateSelector to TemplateType dependency property of item. and each time TemplateType property changed, DataTemplate change automaticaly. How can I do.
I Search about own question. and find below code to refresh ItemTemplateSelector :
DataTemplateSelector DataTemplateSelector = plan.ItemTemplateSelector;
plan.ItemTemplateSelector = null;
plan.ItemTemplateSelector = DataTemplateSelector;
I'd go for a different solution in your case, if you want to update the datatemplate.
I would create a binding to the ItemTemplate of your ItemsControl. That binding gets a converter assigned, which contains the logic of your current ItemTemplateSelector. And the source of the binding is your TemplateType property.
The ItemTemplateSelector is just for one-shot evaluation.
Related
I'm using MVVM pattern and C#, I public a ReadOnlyCollection property from View-Model,
In View, there is a TabControl that ItemsSource property is Binding to this ReadOnlyCollection Property.
How can I know which item is being selected from View-Model?
If ReadOnlyCollection cannot do this, what type should i use?
Thank you.
You can bind to the SelectedItem property of the TabControl to an additional property in your ViewModel.
Check MSDN for all the properties of TabControl.
I'm trying to bind combobox editor in a PropertyGrid to a list.
<dxprg:PropertyGridControl SelectedObject="{Binding SelectedEmployee}">
<dxprg:PropertyDefinition Path="EmployeeCountryID">
<dxprg:PropertyDefinition.EditSettings>
<dxe:ComboBoxEditSettings
ItemsSource="{Binding Path=DataContext.Countries, ElementName=rootWindow}"
ValueMember="CountryId" DisplayMember="CountryName" />
</dxprg:PropertyDefinition.EditSettings>
</dxprg:PropertyDefinition>
</dxprg:PropertyGridControl>
This example is from a third-party control but the problem may be just general.
The "rootWindow" DataContext has been set to a ViewModel which holds a property List(of Country) that I want have as ItemsSource in a Combobox.
I was trying to access that list by setting the Combobox ItemsSource to the rootWindow.DataContext.Countries property but I don't get any data.
Tried also all those RelativeSource FindAncestor bindings but no data appeared either.
Why can't I bind through a DataContext of a given element like this?
This became solved. The problem was not with the binding at all but realated to how I defined the third-party control: Instead of EditSettings I should have defined CellTemplate -> DataTemplate.
Does anybody saw control like this somewhere?
I need to make such control to represent hierarchical data (it should be generic very likely, i.e. data binding, templates support).
Something like combination of ComboBox and MenuItem’s.
I think I will redefine the combobox itemtemplate with some hierarchicaldatatemplate along with popup class.
Just put ComboBoxes on a form and bind the ItemsSource to the top level collection.
Then bind the DataContext of the next ComboBox to the SelectedItem of the box on the left and bind its ItemSource to the collection of items.
You know how to bind to SelectedItem?
E.G.
Column1
Public String Name
Public List Column2s
So you bind the first combox to List with he displaymemberpath = name
Then on the second combobox you bind to Column1 selecteditem with items source path of Column2s
The trick is to build up the Lists within the Lists within the Lists
All right, I made it by custom control inherited from ComboBox, custom ComboBoxItem inherited from HeaderedItemsControl and using HierarchicalDataTemplate.
I have a view (MainPage.xaml) which is bound to a ViewModel.
In the ViewModel I have a DataModel property (note that both viewmodel & datamodel implements INotifyPropertyChanged, or what that interface is called).
In my view i have defined an ItemsControl whose ItemsSource is bound to the said property. This property (DataModel) has an ObservableCollection (which i know it populated with valid data).
The xaml snippet looks like this:
<ItemsControl ItemsSource="{Binding Path=CurrentDataModel.Items}">
Note that i am not showing the rest of the xaml. All it shows is the ItemsTemplate which is just a TextBlock.
The issue is that nothing is being drawn for this ItemsControl, even though i can clearly see (while debugging) that the collection has good data.
Is this "Path=..." binding not possible for an ItemsControl's ItemsSource?
The Path syntax looks correct. The most likely cause of failure is that either the DataContext is not set to your ViewModel or that the property path you specified is incorrect.
In your question you state that the ViewModel has a property named DataModel but in the xaml snippet you have CurrentDataModel.
In my Silverlight 4 application, I have a ContentControl with its ContentTemplate property bound to a property in the data context. That works fine. However, the content of the template once rendered has its DataContext set to null. I would like the content to inherit the same DataContext as set for the ContentControl. Is there a way to get this to happen?
The ContentControl's template has the ContentControl's Content property as a DataContext. So try
<ContentControl Content="{Binding}" />
if this is merely the current DataContext.
I found an alternate way to accomplish what was required. In my case, the template (not the content template) of the ContentControl was unimportant, so I made my DataTemplate objects into ControlTemplate objects instead and bound the Template property of the ContentControl instead of ContentTemplate. The data context was preserved if I did it this way.