用户控件不同类型的WPF名单 [英] WPF List of Different Types of User Controls
问题描述
我有一个WPF列表框,里面叫洁悠神。一个用户控件
这伟大工程,因为我很新的WPF这已经是非常IM pressive。我想现在要做的就是在基于绑定属性列表中不同的用户控件。
这可能吗?如果不是这样,我应该怎么回事实现这一目标?
我使用一个名单,因为我想允许用户控制滴/拖拽排序,并且会有一个可变号码,以便似乎是有道理的 - 各种途径,欢迎
< ListBox的X:名称=peopleListBox
的HorizontalAlignment =拉伸
VerticalAlignment =拉伸
ItemContainerStyle ={的StaticResource ListBoxItemStretch}
前景=透明
BorderBrush =透明
背景=透明
Grid.ColumnSpan =2的SelectionChanged =peopleListBox_SelectionChanged>
< ListBox.ItemTemplate>
<的DataTemplate>
<电网>
<我:洁悠神保证金=4>< /我:洁悠神>
< /网格>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
您可以使用的 DataTemplateSelector ,在<一个href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.selecttemplate.aspx"相对=nofollow> SelectTemplate()方法,你可以检查哪些DataTemplate中,以用于项目公司目前通过:
在XAML:
&LT;! - 定义资源模板
ChartDataTemplate是一个ChartDataTemplate.xaml,相同的其他
- &GT;
&LT; UserControl.Resources&GT;
&LT; DataTemplate中X:关键=ChartDataTemplate&GT;
&LT;的观点:LineChartView /&GT;
&LT; / DataTemplate中&GT;
&LT; DataTemplate中X:关键=GridDataTemplate&GT;
&LT;的观点:PieChartView /&GT;
&LT; / DataTemplate中&GT;
&LT; /UserControl.Resources>
&LT;! - ListView控件的ItemTemplate应指向模板选择 - &GT;
&LT; ItemsControl.ItemTemplate&GT;
&LT;的DataTemplate&GT;
&LT;内容presenter
ContentTemplateSelector ={的StaticResource MyTemplateSelector}&GT;
在$后面C $ C:
专用密封类MyTemplateSelector:DataTemplateSelector
{
公众覆盖的DataTemplate SelectTemplate(
对象的项目,
DependencyObject的容器)
{
// 1.案例项目的对象是绑定到每个ListView项
// 2.基于对象类型/状态返回正确的DataTemplate
//为this.Resources [ChartDataTemplate]或
// this.Resources [GridDataTemplate]
}
}
I have a WPF listbox that houses a user control called JUC.
This works great and as I'm very new to WPF this already is very impressive. What I would like to do now is have different user controls in the list based upon a bound property.
Is this possible? If not, how else should I achieve this?
I'm using a list because I want to allow drop/drag ordering of the user controls, and there will be a variable number so seems to make sense - alternative approaches are welcome.
<ListBox x:Name="peopleListBox"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemContainerStyle="{StaticResource ListBoxItemStretch}"
Foreground="Transparent"
BorderBrush="Transparent"
Background="Transparent"
Grid.ColumnSpan="2" SelectionChanged="peopleListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<my:JUC Margin="4"></my:JUC>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You can use DataTemplateSelector, in SelectTemplate() method you can check which DataTemplate to use for currently passed in item:
In XAML:
<!-- define templates in resources
ChartDataTemplate is a ChartDataTemplate.xaml, the same for other
-->
<UserControl.Resources>
<DataTemplate x:Key="ChartDataTemplate">
<views:LineChartView />
</DataTemplate>
<DataTemplate x:Key="GridDataTemplate">
<views:PieChartView />
</DataTemplate>
</UserControl.Resources>
<!-- ListView Itemtemplate should point to template selector -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentPresenter
ContentTemplateSelector = "{StaticResource MyTemplateSelector}">
In Code behind:
private sealed class MyTemplateSelector: DataTemplateSelector
{
public override DataTemplate SelectTemplate(
object item,
DependencyObject container)
{
// 1. case item to your object which is bound to each ListView item
// 2. based on object type/state return correct DataTemplate
// as this.Resources["ChartDataTemplate"] or
// this.Resources["GridDataTemplate"]
}
}
这篇关于用户控件不同类型的WPF名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!