【发布时间】:2016-04-13 09:21:43
【问题描述】:
这个快把我逼疯了……
这是我用来绑定数据网格中列中的组合框的 xaml。 ItemSource 指向持有“Pipetter”类的 ObservableCollection。 CellTemplate,只需要显示当前选中行的这个 Pipetter 类的“name”属性。
问题是,一旦我在组合框中选择了一个值,所选择的值就会突然出现在该列的所有行中。我已经以许多不同的方式重新设计了这些方法,并且在每种情况下都发生了这种情况。关于关闭什么设置的任何想法?
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
IsEditable="False"
ItemsSource="{Binding DataContext.Pipettors, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Mode=TwoWay}"
SelectedItem="{Binding DataContext.SelectedPipettor, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding TipGroup.PipettorTipType.Pipettor}"
DisplayMemberPath="Name"
/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label
Content="{Binding TipGroup.PipettorTipType.Pipettor.Name}"
Style="{DynamicResource DataGridRowLabel}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
这是选定项绑定到的 Vm 中的属性。我只是将“SelectedItem”分配给当前选定行(SelectedTipGroup)中的相应属性。这在 DataGrid 定义中被定义为“SelectedItem”。
private Pipettor selectedPipettor;
public Pipettor SelectedPipettor
{
get { return selectedPipettor; }
set
{
SetProperty(ref selectedPipettor, value);
SelectedTipGroup.TipGroup.PipettorTipType.Pipettor = value;
}
}
我按照建议更新了组合框绑定:
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="PipetterComboBox"
ItemsSource= "{Binding DataContext.Pipettors, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=TwoWay}"
SelectedItem="{Binding TipGroup.PipettorTipType.Pipettor}" IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="Name"
/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding TipGroup.PipettorTipType.Pipettor.Name}" Style="{DynamicResource DataGridRowLabel}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
当在数据网格的一行中进行选择时,相同的值出现在该列的所有行中......它只是试图将 selectedItem 类分配给当前行中的类属性“Pipettor”。 ..
在这个上花了 DAYS 天.. 没有意义...
谢谢!
这是组合框绑定到的属性。组合框的 ItemsSource 只是 Pipettor 类型的 observablecollection。
private Pipettor pipettor;
[DataMember]
public Pipettor Pipettor
{
get { return this.pipettor; }
set
{
if (SetProperty(ref this.pipettor, value))
{
//***BKM This was failing on delete - not sure if masking or not but will null check
//note something similar in other models - review
if (value != null)
{
this.pipettorId = this.pipettor.Identity;
}
}
}
}
和 SetProperty()
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (object.Equals(field, value))
{
return false;
}
field = value;
//Check to make sure Tracking is Initialized but no need to do anything
if (!this.Tracking.Initialized)
{
}
RaisePropertyChanged(propertyName);
return true;
}
【问题讨论】:
-
也许在你的属性设置器中通知 IPropertyChanged?
-
SetProperty 是做什么的? PropertyChanged 有没有机会被解雇?
-
setproperty 只是封装了检查以查看新值是否为 != 当前值,如果是,则引发 propertyChanged 事件并将支持字段设置为新值。
标签: wpf data-binding wpfdatagrid