【发布时间】: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 属性是在公司工作的所有人员的列表。一个人有两个属性Name和Symbol(他们的脸部图像)。
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 应该如何工作?您想将所选项目存储在哪里?