C# 将DataTemplate中的ComboBox SelectedItem设置为自身
因此,我对组合框中的C# 将DataTemplate中的ComboBox SelectedItem设置为自身,c#,wpf,binding,C#,Wpf,Binding,因此,我对组合框中的SelectedItem有一个小问题 首先,我有一个用于组合框内项目的模板。这是因为我想为每个项目显示一个图标和一个文本 <DataTemplate x:Key="PersonsComboBox" DataType="asi:Person"> <WrapPanel> <StackPanel Orientation="Horizontal"> <Image Width="40" Heigh
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>
属性AllPersons
是在公司工作的所有人员的列表。一个人有两个属性姓名
和符号
(脸部图像)
最后,我想显示很多组合框(使用ItemsControl)。每个组合框代表一个人。但我想换人。SelectedItem应该是SelectedDepartment.Persons中的项目
<StackPanel>
<ItemsControl ItemsSource="{Binding SelectedDepartment.Persons, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplateSelector="{StaticResource PersonTemplateSelector}"/>
</StackPanel>
因此,基本上我想要的是将组合框的SelectedItem设置为自身,以便SelectedItem=“{Binding}”。但是ItemSource是另一个。它们只是具有相同的数据类型我建议您重新设计数据,以便在单独的视图模型中支持人员选择
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; }
}
类多人容器
{
public ICollection AllPersons{get;}//所有可选person对象的列表
public ICollection DisplayedPersons{get;}//可从组合框中选择实际人员的显示人员列表
}
类个人集装箱
{
public Person SelectedPerson{get;set;}//应显示其属性的人员
}
班主任
{
公共字符串名称{get;set;}
公共图像符号{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
模板中的
不是很有用Action类是否有Actions属性?请发布您的类定义。它本身=DataContext
?SelectedItem=“{Binding}”
是否有效(更有可能是单向
/一次性
绑定)?我猜你只需要在操作中重写Equals()
@mm8我更新了我的问题,以便更容易理解我的意思。一个人没有AllPersons属性,那么你的PersonTemplate应该如何工作呢?您希望将所选项目存储在何处?
<StackPanel>
<ItemsControl ItemsSource="{Binding SelectedDepartment.Persons, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplateSelector="{StaticResource PersonTemplateSelector}"/>
</StackPanel>
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; }
}
<!-- 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>