DataBinding不适用于DataGrid中的Combobox

时间:2016-01-08 16:10:28

标签: wpf data-binding wpfdatagrid

这个让我疯狂......

这是我用来绑定数据网格中列的组合框的xaml。 ItemSource是一个ObservableCollection,它持有类" Pipetter"。 CellTemplate,只需要显示" name"此Pipetter类的属性,用于当前选定的行。

问题是,只要我在组合框中选择一个值,所选的值就会突然出现在该列的所有行中。我已经用这么多不同的方式重做了工作,并且在每种情况下都会发生这种情况。关于什么设置关闭的任何想法?

<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绑定的属性。我只是选择&#34; SelectedItem&#34;并将其分配给当前所选行(SelectedTipGroup)中的相应属性。这被定义为&#34; SelectedItem&#34;在DataGrid定义中。

 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类分配给类属性&#34;移液器&#34;在当前行...

在这个上花了DAYS ......毫无意义......

谢谢!

这是组合框绑定的属性。组合框的ItemsSource只是Pipettor类型的可观察集合。

  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;
    }

1 个答案:

答案 0 :(得分:0)

ComboBox的SelectedItem使用RelativeSource来使用DataGrid的DataContext,因此所有行都使用相同的SelectedItem。将其更改为DataGrid中每行的项目中的绑定。看起来你可能已经在SelectedValue上有这个绑定了,看起来它应该真的在SelectedItem上。

SelectedValue绑定不起作用,因为未设置SelectedValuePath。有关SelectedItem和SelectedValue之间的区别,请参阅此问题: Difference between SelectedItem, SelectedValue and SelectedValuePath

编辑:根据此答案更新问题后,在{xaml}中为ComboBox添加了IsSynchronizedWithCurrentItem="True"。来自MSDN的此属性文档:

  

&#34;您可以将IsSynchronizedWithCurrentItem属性设置为true,以确保所选项始终对应于ItemCollection中的CurrentItem属性。例如,假设有两个ListBox控件,其ItemsSource属性设置为相同的源。在两个列表框中将IsSynchronizedWithCurrentItem设置为true,以确保每个ListBox中的选定项都相同。&#34;

将此设置为true,这也会导致所有ComboBox使用相同的选择。从xaml中为ComboBox删除此属性。

MSDN参考:https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.issynchronizedwithcurrentitem(v=vs.110).aspx

相关问题