Tur*_*rbo 4 data-binding wpf xaml
在WPF中是否有某种方法可以获得相同的功能DataTemplateSelector,但对于UserControls?
假设我有一个StackView,我想绑定一个IEnumerable对象.我想要做的是以某种方式有一个映射,对于绑定的IEnumerable中的每个对象类型,查看对象类型并确定要添加到StackView的UserControl.
所以,给出三个类:
public class House : Building{}
public class Apartment : Building{}
public class Tent : Building{}
Run Code Online (Sandbox Code Playgroud)
在每个类继承Building并有自己定义的地方UserControl,我想设置DataContext为一个IEnumerable<Building>并以某种方式让StackView用特定类型的UserControl填充它的子集.
我想用尽可能少的代码来做到这一点.数据绑定和XAML管道胶带越多越好.
您可以在一个DataTemplate;中使用复杂的用户控件; 只是声明DataTemplate你的UserControl.
例:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="300" Width="300" Name="UI" >
<Window.Resources>
<DataTemplate DataType="{x:Type local:House}" >
<local:HouseUserControl DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Apartment}">
<local:ApartmentUserControl DataContext="{Binding}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding ElementName=UI, Path=ListOfBuildings}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1867 次 |
| 最近记录: |