绑定到集合时的条件数据模板 [英] Conditional DataTemplates when binding to a collection

查看:21
本文介绍了绑定到集合时的条件数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例应用:

所提供代码所属的示例应用程序通过绑定显示了一个 Vehicle 对象列表.Vehicle 类是一个顶级类,子类可以从中派生,例如汽车自行车.此时的示例应用程序显示 Vehicle 的所有者名称.

The sample application that the supplied code belongs to displays a list of Vehicle objects via Binding. The Vehicle class is a top level class that subclasses can derive from e.g. Car and Bike. The sample application at the moment displays the owner's name of the Vehicle.

示例模型代码:

public class Vehicle
{
    private string _ownerName;
    public string ownerName
    {
        get { return _ownerName; }
        set { _ownerName = value; }
    }
}

public class Car : Vehicle
{
    public int doors;
}

public class Bike : Vehicle
{
    // <insert variables unique to a bike, ( I could not think of any...)>
}

用户控件 XAML 代码:

UserControl XAML Code:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="itemTemplate">
            <WrapPanel>
                <TextBlock Text="{Binding Path=ownerName}"/>
            </WrapPanel>
        </DataTemplate>
    </Grid.Resources>
    <ListBox x:Name="list" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" ItemsSource="{Binding}" ItemTemplate="{StaticResource itemTemplate}" />
</Grid>

背后的用户控制代码:

    public List<Vehicle> vehicleList = new List<Vehicle>();

    public CustomControl()
    {
        InitializeComponent();
        createSomeVehicles();
        list.DataContext = vehicleList;
    }

    public void createSomeVehicles()
    {
        Car newcar = new Car();
        newcar.doors = 5;
        newcar.ownerName = "mike";

        Bike newbike = new Bike();
        newbike.ownerName = "dave";

        vehicleList.Add(newcar);
        vehicleList.Add(newbike);
    }

我希望能够做什么:

我希望能够根据 Vehicle 对象的类型在列表对象中显示一个按钮.例如.我想在 Car 的列表项中显示一个 Open Boot 按钮;类型 Bike 没有引导,因此列表项中不会显示任何按钮.

I would like to be able to display a button in the list object dependant upon the Type of the Vehicle object. E.g. I would like to display a Open Boot button within the list item for Car's; Type Bike does not have a boot and so no button would display within the list item.

关于如何实现这一点的想法:

我根据对象的类型研究了不同 DataTemplate 的自定义绑定.例如.从后面的代码我可以调用:

I have looked into the custom binding of different DataTemplates based upon what type of object it is. E.g. from the code behind I could call:

object.Template = (ControlTemplate)control.Resources["templateForCar"];

这里的问题是我在整个列表上使用了 Binding,因此无法手动将 DataTemplate 绑定到每个列表项,列表绑定控制其项的 DataTemplate.

The problem here is that I am using a Binding on the whole list and so there is no way to manually bind a DataTemplate to each of the list items, the list binding controls the DataTemplate of it's items.

推荐答案

您可以为每辆自行车和汽车(以及任何 CLR 类型)创建一个 DataTemplate.通过指定 DataTemplateDataType 属性,只要 WPF 看到该类型,就会自动应用该模板.

You can create a DataTemplate for each Bike and Car (and for any CLR type). By specifying the DataTemplate's DataType property, the template will automatically be applied whenever WPF sees that type.

<Grid>
    <Grid.Resources>
        <DataTemplate DataType="{x:Type local:Car}">
            <WrapPanel>
                <TextBlock Text="{Binding Path=ownerName}"/>
                <Button Content="Open Boot" ... />
            </WrapPanel>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Bike}">
            <WrapPanel>
                <TextBlock Text="{Binding Path=ownerName}"/>
            </WrapPanel>
        </DataTemplate>
    </Grid.Resources>
    <ListBox x:Name="list" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" ItemsSource="{Binding}" />
</Grid>

这篇关于绑定到集合时的条件数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆