2016-01-08 78 views
0

这一个不工作的组合框让我发疯了...数据绑定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; 
    } 
+0

也许在你的财产的二传手通知IPropertyChanged? – ProgrammingDude

+0

SetProperty是做什么的? PropertyChanged在那里解雇了吗? –

+0

setproperty只是封装检查以查看新值是否为!=当前值,如果是则引发propertyChanged事件并将后台字段设置为新值。 –

回答

0

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

SelectedValue绑定不能按原样工作,因为未设置SelectedValuePath。看到这个问题的的SelectedItem和的SelectedValue的区别: Difference between SelectedItem, SelectedValue and SelectedValuePath

编辑:这个问题是每这个答案更新后,IsSynchronizedWithCurrentItem="True"在XAML组合框加入。从MSDN文档此属性:

“你可以设置IsSynchronizedWithCurrentItem属性为true,以确保始终选择的项目对应于ItemCollection的CURRENTITEM属性。例如,假设有两个列表框控件的ItemsSource属性设置为相同的源。在两个列表框中将IsSynchronizedWithCurrentItem设置为true,以确保每个ListBox中的选定项目都是相同的。

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

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

+0

我试过这样做,当我选择下拉值时,它仍在更新所有行: 请参阅编辑... –

+0

您要绑定的TipGroup.PipettorTipType.Pipettor的代码是什么样的? – JNP

+0

您在更新的xaml中添加了IsSynchronizedWithCurrentItem =“True”。这被设计为将具有相同ItemsSource的多个选择同步到相同的SelectedItem。删除。 – JNP