如何基于值选择ItemTemplate?

时间:2019-05-09 21:13:21

标签: c# wpf

我有一组对象显示在ItemsControl中,并且要根据每个对象中的值显示不同的ItemTemplate / DataTemplate。我知道您可以使用不同的对象类型来执行此操作,但是可以基于值来执行此操作吗?

public class MyItem {
    public int MyValue { get; set; }
}

public ObservableCollection<MyItem> MyItems { get; set; }

<ItemsControl ItemsSource="{Binding Path=MyItems}">

<!--If MyValue == 1-->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBox/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

<!--If MyValue == 2-->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <CheckBox/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

<!--If MyValue == 3-->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <ComboBox/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>

2 个答案:

答案 0 :(得分:2)

是的,您将为此使用触发器。

<ItemsControl ItemsSource="{Binding Path=MyItems}">
    <ItemsControl.Style>
        <Style TargetType="ItemsControl">
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid>
                                <!--default template-->
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                <DataTrigger Binding="{Binding MyValue}" Value="1">
                    <Setter Property="ItemTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Grid>
                                    <TextBox/>
                                </Grid>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding MyValue}" Value="2">
                    <Setter Property="ItemTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Grid>
                                    <CheckBox/>
                                </Grid>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding MyValue}" Value="3">
                    <Setter Property="ItemTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Grid>
                                    <TextBox/>
                                </Grid>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.Style>
</ItemsControl>

答案 1 :(得分:2)

首先,如果您只需要使用样式触发器即可(如Neil B的回答),就可以。如果您的价值更复杂,则可能需要其他选择...

当您有复杂的值要打开模板时,可以使用DataTemplateSelector。这会增加一些开销/复杂性,但会打开您的选择。

public class MyDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element != null && item != null && item is MyItem myItem)
        {
            switch (myItem.MyValue)
            {
                case 1:
                    return element.FindResource("TextBoxResource") as DataTemplate;
                case 2:
                    return element.FindResource("CheckBoxResource") as DataTemplate;
                case 3:
                    return element.FindResource("ComboBoxResource") as DataTemplate;
            }
        }

        return null; // or provide a default template
    }
}

然后在您的XAML中:

<App.Resources>
    <!-- these resources can be in any context such as app, window, or user control, they just need to be in scope -->
    <DataTemplate x:Key="TextBoxResource">
        <Grid>
            <TextBox/>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="CheckBoxResource">
        <Grid>
            <CheckBox/>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="ComboBoxResource">
        <Grid>
            <ComboBox/>
        </Grid>
    </DataTemplate>

    <MyDataTemplateSelector x:Key="myDataTemplateSelector"/>
</App.Resources>

<ItemsControl ItemsSource="{Binding Path=MyItems}"
              ItemTemplateSelector="{StaticResource myDataTemplateSelector}">
</ItemsControl>

然后,数据模板选择器将明确选择要提供给ItemsControl的DataTemplate,而ItemsControl的ItemTemplateSelector属性与ItemTemplate属性互斥(不能同时设置两者)。