WPF:SelectedItem 绑定不显示 DisplayMemberPath
WPF: SelectedItem Binding Not Showing With DisplayMemberPath
我有一个 ComboBox(在 ListView 内),它曾经与字符串集合相关联。但是,我正在改用自定义 class。
现在 ComboBox 绑定到 Area 类型的 ObservableCollection。为了显示,我使用 DisplayMemberPath 显示名称 属性。这很好用,但是盒子不再加载当前选择的值。
<ListView x:Name="TestListView" ItemsSource="{Binding TestViewList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListView.View>
<GridView>
<GridViewColumn Header="Area">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.AreaList, ElementName=BVTWindow}" DisplayMemberPath="Name" SelectedItem="{Binding Path=Area, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
public ObservableCollection<ViewTest> TestViewList { get; private set; }
public ObservableCollection<Area> AreaList { get; private set; }
public class ViewTest : BindableBase
{
public string Description { get; set; }
public Area Area { get; set; }
}
public partial class Area
{
public long ID { get; set; }
public string Name { get; set; }
}
"Area" 与 Collection 相同 class,并且是 ListView 的 ItemsSource 绑定到的 class 的 属性。我这样做是对的吗?非常感谢任何帮助。
更新:
我有一个理论,也许使用 "Name" 属性(这是一个字符串)在框中显示会使 SelectedItem 属性查找字符串而不是 Area 类型的东西。我更改了 TestViewList 的 class 以使用字符串来跟踪区域,但这根本没有改变程序的行为。
更新 2:
在上面,我添加了与相关 ComboBox 和 ListView 相关的视图模型中的相关行。
更新 3:
我已将我的区域 属性 从内联更改为扩展,包括处理提升的 SetProperty。它现在适用于在 运行 时添加到 ItemSource 的新项目,但对于在程序启动时加载的项目仍然是空白选择。
public class ViewTest : BindableBase
{
public string Description { get; set; }
public Area Area
{
get
{
return this.area;
}
set
{
this.SetProperty(ref this.area, value);
}
}
}
更新四:
事实证明,Paul Gibson 的回答是正确的。我的现有条目未正确加载的原因与我从数据库加载项目的方式中的逻辑错误有关,而不是 xaml 绑定的问题。现在一切正常(至少对于那些组合框而言)。
根据您所说的,我认为我的评论就是答案。这是单个组合框的情况。在您的情况下,网格的每一行都需要一个,因此您可能必须以编程方式按索引将绑定添加到列表的成员。
在你的视图模型中,如果你也有(单例):
private Area _curSelArea;
public Area curSelArea
{
get { return _curSelArea; }
set
{
_curSelArea = value;
RaisePropertyChanged("curSelArea");
}
}
然后您可以使用以下方式绑定到 属性:
SelectedItem="{Binding DataContext.curSelArea . . . }"
如果最初已知 curSelArea 的初始值,视图模型可以设置它。
编辑:在实际必须这样做之后,我发现有人扩展了 DataGridComboBoxColumn 以促进更好的绑定。如果您正在尝试这样做,请查看此 link:http://joemorrison.org/blog/2009/02/17/excedrin-headache-35401281-using-combo-boxes-with-the-wpf-datagrid/
我有一个 ComboBox(在 ListView 内),它曾经与字符串集合相关联。但是,我正在改用自定义 class。
现在 ComboBox 绑定到 Area 类型的 ObservableCollection。为了显示,我使用 DisplayMemberPath 显示名称 属性。这很好用,但是盒子不再加载当前选择的值。
<ListView x:Name="TestListView" ItemsSource="{Binding TestViewList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListView.View>
<GridView>
<GridViewColumn Header="Area">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.AreaList, ElementName=BVTWindow}" DisplayMemberPath="Name" SelectedItem="{Binding Path=Area, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
public ObservableCollection<ViewTest> TestViewList { get; private set; }
public ObservableCollection<Area> AreaList { get; private set; }
public class ViewTest : BindableBase
{
public string Description { get; set; }
public Area Area { get; set; }
}
public partial class Area
{
public long ID { get; set; }
public string Name { get; set; }
}
"Area" 与 Collection 相同 class,并且是 ListView 的 ItemsSource 绑定到的 class 的 属性。我这样做是对的吗?非常感谢任何帮助。
更新:
我有一个理论,也许使用 "Name" 属性(这是一个字符串)在框中显示会使 SelectedItem 属性查找字符串而不是 Area 类型的东西。我更改了 TestViewList 的 class 以使用字符串来跟踪区域,但这根本没有改变程序的行为。
更新 2:
在上面,我添加了与相关 ComboBox 和 ListView 相关的视图模型中的相关行。
更新 3:
我已将我的区域 属性 从内联更改为扩展,包括处理提升的 SetProperty。它现在适用于在 运行 时添加到 ItemSource 的新项目,但对于在程序启动时加载的项目仍然是空白选择。
public class ViewTest : BindableBase
{
public string Description { get; set; }
public Area Area
{
get
{
return this.area;
}
set
{
this.SetProperty(ref this.area, value);
}
}
}
更新四:
事实证明,Paul Gibson 的回答是正确的。我的现有条目未正确加载的原因与我从数据库加载项目的方式中的逻辑错误有关,而不是 xaml 绑定的问题。现在一切正常(至少对于那些组合框而言)。
根据您所说的,我认为我的评论就是答案。这是单个组合框的情况。在您的情况下,网格的每一行都需要一个,因此您可能必须以编程方式按索引将绑定添加到列表的成员。
在你的视图模型中,如果你也有(单例):
private Area _curSelArea;
public Area curSelArea
{
get { return _curSelArea; }
set
{
_curSelArea = value;
RaisePropertyChanged("curSelArea");
}
}
然后您可以使用以下方式绑定到 属性:
SelectedItem="{Binding DataContext.curSelArea . . . }"
如果最初已知 curSelArea 的初始值,视图模型可以设置它。
编辑:在实际必须这样做之后,我发现有人扩展了 DataGridComboBoxColumn 以促进更好的绑定。如果您正在尝试这样做,请查看此 link:http://joemorrison.org/blog/2009/02/17/excedrin-headache-35401281-using-combo-boxes-with-the-wpf-datagrid/