Is it possible to cancel a WPF Binding to a Control.Template property using Binding.DoNothing?
I want to easily use a read only template for a control when the value of a property in my model is true. To do that I've created a IValueConverter which returns it's parameter (a template in this case) when the source value is true, and Binding.DoNothing when false.
开发者_JAVA百科When I apply this to my control, I get a control without Template.
It sounds like you could use a DataTemplateSelector rather than IValueConverter, something along the lines of:
//namespace MyProject.ViewUtilities
public class MyDataTemplateSelector: DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var localFrameworkElement = container as FrameworkElement;
var modelObject = item as ModelObject;
if(modelObject.BoolProperty){
return Template(localFrameworkElement, "WhenTrueDataTemplate");
}
else
{
return Template(localFrameworkElement, "WhenFalseDataTemplate");
}
}
private DataTemplate Template(FrameworkElement localFrameworkElement, string resourceKeyString)
{
return localFrameworkElement.FindResource(resourceKeyString) as DataTemplate;
}
}
Used something like:
<ComboBox xmlns:mpvu="MyProject.ViewUtilities"
ItemsSource="{Binding Path=MyModelObjectCollection}">
<ComboBox.ItemTemplateSelector>
<mpvu:MyDataTemplateSelector/>
</ComboBox.ItemTemplateSelector>
</ComboBox>
If this isn't what you want, perhaps add some sample code to your post.
精彩评论