这一个不工作的组合框让我发疯了...数据绑定DataGrid中
这里是我使用绑定的组合框是在一个DataGrid列中的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>
这是selectedItem绑定到的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>
而且仍然在进行选择时在DataGrid中的一个行所作,同样的值将出现在所有行该列......这是只是想给将selectedItem类分配给当前行中的类属性“移液器” ......
曾花费数天时间在这一个..没有任何意义......
谢谢!
这是组合框绑定到的属性。组合框的ItemsSource只是类型移液管的可观察集合。
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? – ProgrammingDude
SetProperty是做什么的? PropertyChanged在那里解雇了吗? –
setproperty只是封装检查以查看新值是否为!=当前值,如果是则引发propertyChanged事件并将后台字段设置为新值。 –