wpf usercontrol collection properties with different types
I have a user control that has a few collection properties. they show up in the properties table at design time and provide me the collection property editor when I click the ellipse. all of this is working so far.
I have a control with a collection property that I want to be able to use different types, just like how the datagrid allows you to select DataGridTextColumn, DataGridCheckBoxColumn etc in its 开发者_Python百科column collection property. I thought this would be a simple thing to accomplish by creating the public browsable property of an abstract base class type and it would allow all my derived classes in the list. but its not working. if I make the base class non-abstract it shows up, but I can't get more than one type to show up in that list. how do I make it work?
If I am understanding your question correctly, what you want is an implementation of a DataTemplateSelector, and a set of DataTemplate resources according to the Types of properties you want to render.
Something on these lines:
public class TemplateSelector:DataTemplateSelector
{
public override DataTemplate SelectTemplate
(object item,DependencyObject container)
{
var element = container as FrameworkElement;
if(element is ClassA)
return element.FindResource("classATemplate") as DataTemplate;
if(element is ClassB)
return element.FindResource("classBTemplate") as DataTemplate;
return null;
}
}
Where ClassA and ClassB are your custom types.
For the alternation index, you can create a style on these lines
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="LightBlue"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGreen"></Setter>
</Trigger>
</Style.Triggers>
精彩评论