2

在 WPF 项目中,我可以ComboBox从不同的对象中进行选择。使用 an ItemsControland it'sItemTemplateSelector我试图ComboBox根据对象的属性为选择显示不同的 UI。因此,在下面的示例中,我们从人员对象中进行选择。在ItemTemplateSelector我们DataTemplate根据Person'IsManager属性选择不同的。问题是它不起作用。

我怀疑这可能是由于ItemsSourceItemsControl绑定到一个项目,但不确定?如果这是问题所在,任何人都可以建议如何更改代码或我可以实现上述目的的其他方式吗?

提前谢谢了。

XAML:

<Window x:Class="ItemsSelector.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ItemsSelector"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.Resources>
        <local:Selector x:Key="selector"/>

        <DataTemplate x:Key="managerTemplate">
            <TextBlock Text="Manager"/>
        </DataTemplate>

        <DataTemplate x:Key="juniorTemplate">
            <TextBlock Text="Junior"/>
        </DataTemplate>

    </Grid.Resources>

    <ComboBox x:Name="cbo" Margin="2" Grid.Row="0" ItemsSource="{Binding .}" SelectedIndex="0">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <ItemsControl Grid.Row="1" ItemTemplateSelector="{StaticResource selector}" ItemsSource="{Binding ElementName=cbo ,Path=SelectedItem}">

    </ItemsControl>
</Grid>

代码背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new Person[] {
            new Person() { Name = "Boss", IsManager = true },
            new Person() { Name = "Underling", IsManager = false }
        };
    }
}

人:

public class Person
{
    public string Name { get; set; }
    public bool IsManager { get; set; }
    public string Title { get; set; }
}

选择器:

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

        if (element != null && item != null && item is Person)
        {

            var person = item as Person;

            switch (person.IsManager)
            {
                case true:
                    return element.FindResource("managerTemplate") as DataTemplate;

                case false:
                    return element.FindResource("juniorTemplate") as DataTemplate;

                default:
                    break;
            }
        }

        return null;
    }
}
4

2 回答 2

2

an的ItemsSource属性ItemsControl只能绑定到返回 an 的集合IEnumerable

您应该使用 aContentControl能够绑定显示的选定项ComboBox

<ContentControl Grid.Row="1" ContentTemplateSelector="{StaticResource selector}" 
                Content="{Binding ElementName=cbo ,Path=SelectedItem}">
</ContentControl>
于 2017-03-07T15:58:50.320 回答
0

我想我已经找到了解决方案。我需要使用ContentTemplateSelector.

于 2017-03-07T15:57:22.670 回答