2016-01-08 62 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