【问题标题】:Set ComboBox SelectedItem in DataTemplate to itself将 DataTemplate 中的 ComboBox SelectedItem 设置为自身
【发布时间】:2017-11-09 13:25:59
【问题描述】:

所以我对组合框中的SelectedItem 有一个小问题。

首先,我有一个用于组合框内项目的模板。这是因为我想为每个项目显示一个图标和一个文本。

<DataTemplate x:Key="PersonsComboBox" DataType="asi:Person">
    <WrapPanel>
        <StackPanel Orientation="Horizontal">
            <Image Width="40" Height="30" Source="{Binding Path=Symbol, Converter={StaticResource ImageConverter}}" />
            <vw:Label Content="{Binding Name}" />
        </StackPanel>
    </WrapPanel>
</DataTemplate>

如您所见,此模板来自 DataType Person

现在,我在另一个 DataTemplate 中使用这个 ComboBox。数据模板如下所示:

<DataTemplate x:Key="PersonsTemplate" DataType="asi:Person">
    <vw:ComboBox ItemTemplate="{StaticResource PersonsComboBox}" ItemsSource="{Binding AllPersons}" SelectedItem="???">
</DataTemplate>

AllPersons 属性是在公司工作的所有人员的列表。一个人有两个属性NameSymbol(他们的脸部图像)。

AllPersons = new List<Person>
{
    new Person { Name = "Jenny", Symbol = new Image.FromFile("Path") },
    new Person { Name = "Mike", Symbol = new Image.FromFile("Path") }
    new Person { Name = "Peter", Symbol = new Image.FromFile("Path") }
    new Person { Name = "Nicole", Symbol = new Image.FromFile("Path") }
}

人:

public class Person
{
    public string Name { get; set; }

    public Image Symbol { get; set; }
}

最后我想显示很多组合框(使用 ItemsControl)。每个 ComboBox 代表一个人。但我希望能够换人。 SelectedItem 应该是 SelectedDepartment.Persons 中的项目

<StackPanel>
    <ItemsControl ItemsSource="{Binding SelectedDepartment.Persons, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplateSelector="{StaticResource PersonTemplateSelector}"/>
</StackPanel>

所以基本上我想要的是将我的 ComboBox 的 SelectedItem 设置为它本身,这样 SelectedItem="{Binding}"。但 ItemSource 是不同的。它们只是具有相同的 DataType

【问题讨论】:

  • Action 类是否有 Actions 属性?请发布您的课程定义。
  • 本身 = DataContext? SelectedItem="{Binding}" 是否有效(更有可能是 OneWay/OneTime 绑定)?我猜你只需要在Action 中覆盖Equals()
  • @mm8 我已经更新了我的问题,以便更容易理解我的意思
  • A Person 没有 AllPersons 属性,那么您的 PersonsTemplate 应该如何工作?您想将所选项目存储在哪里?

标签: c# wpf binding


【解决方案1】:

我建议您重新设计数据以支持在单独的视图模型中选择人员。

class MultiplePeopleContainer
{
    public ICollection<Person> AllPersons { get; } // list of all selectable person objects

    public ICollection<PersonContainer> DisplayedPersons { get; } // list of displayed persons where the actual person can be selected from combobox
}

class PersonContainer
{
    public Person SelectedPerson { get; set; } // person whos properties should be displayed
}

class Person
{
    public string Name { get; set; }

    public Image Symbol { get; set; }
}

那么 XAML 就变得容易多了

<!-- same as in question-->
<DataTemplate x:Key="PersonsComboBox" DataType="asi:Person">
    <WrapPanel>
        <StackPanel Orientation="Horizontal">
            <Image Width="40" Height="30" Source="{Binding Path=Symbol, Converter={StaticResource ImageConverter}}" />
            <vw:Label Content="{Binding Name}" />
        </StackPanel>
    </WrapPanel>
</DataTemplate>

<!-- new -->
<DataTemplate DataType="asi:MultiplePeopleContainer">
    <Grid>
        <Grid.Resources>
            <CollectionViewSource x:Key="SelectablePersons" Source="{Binding AllPersons}"/>

            <DataTemplate DataType="asi:PersonContainer">
                <vw:ComboBox ItemTemplate="{StaticResource PersonsComboBox}" ItemsSource="{Binding Source={StaticResource SelectablePersons}" SelectedItem="{Binding SelectedPerson}" />
            </DataTemplate>
        </Grid.Resources>

        <ItemsControl ItemsSource="{Binding DisplayedPersons}"/>
    </Grid>
</DataTemplate>

旁注:PersonsComboBox 模板中的 &lt;WrapPanel&gt; 并不是很有用

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 2019-02-18
    • 2017-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多