【发布时间】:2013-11-12 22:08:50
【问题描述】:
xaml 视图:
<DataGrid x:Name="dgInstances" SelectedItem="{Binding Path=Instance, Mode=OneWay}"
ItemsSource="{Binding Instances}"> SelectionMode="Single"> ... etc.
视图模型:
public List<string> Instances
string instance;
public string Instance
{
get
{
if (instance == null && instances.HasItems())
{
instance = instances[0];
}
if (instance != null)
{
return instance;
}
return null;
}
set
{
instance = value;
}
}
视图模型类实现INotifyPropertyChanged,在获取Instances 后,我给出RaisePropertyChanged("Instance");。
实例在数据网格中正确显示,但未选择第一项。
我不想添加SelectedIndex=0,因为它必须通过绑定。
并且当我在数据网格中更改选择时,未使用数据绑定Instance 的设置器。
这当然也是一个要求。
它是一个只读列表:只有选定项必须是数据绑定的,而不是任何值。
【问题讨论】:
标签: c# wpf data-binding datagrid selecteditem