2016-09-18 252 views
0

我有DataGridComboBoxColumn應該顯示整數或文本「默認」。當我添加行時,combobox從viewmodel的綁定屬性中獲取正確的值,但是當我更改用戶界面中的值時,該屬性的集合不會被調用。我嘗試了SelectedValueBinding和SelectedItemBinding。轉換器的ConvertBack永遠不會被調用。我不應該知道它應該被稱爲。爲什麼更改DataGrid ComboBox值不會更新綁定屬性?

的事物,它們:

  • 的DataGrid的SelectedItem結合
  • 文本列綁定兩種方式(此處省略短促)

這裏是我的代碼:

XAML:

<DataGrid Name="SelectionSetsGrid" CanUserAddRows="False" CanUserResizeColumns="True" CanUserSortColumns="True" 
         ItemsSource="{Binding SelectionSets}" AutoGenerateColumns="False" 
         SelectedItem="{Binding SelectedSelectionSet}"> 
<DataGrid.Columns> 
    <DataGridComboBoxColumn Header="Width" SelectedValueBinding="{Binding LineWidthIndex}"> 
    <DataGridComboBoxColumn.ElementStyle> 
     <Style TargetType="ComboBox" BasedOn="{StaticResource Theme.ComboBox.Style}"> 
      <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.LineWidths}"/> 
      <Setter Property="IsReadOnly" Value="True"/> 
      <Setter Property="ItemTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <WrapPanel> 
          <TextBlock Text="{Binding Converter={StaticResource IntToIntTextOrDefaultConverter}}" VerticalAlignment="Center"/> 
         </WrapPanel> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </DataGridComboBoxColumn.ElementStyle> 
    <DataGridComboBoxColumn.EditingElementStyle> 
     <Style TargetType="ComboBox" BasedOn="{StaticResource Theme.ComboBox.Style}"> 
      <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.LineWidths}"/> 
      <Setter Property="ItemTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <WrapPanel> 
          <TextBlock Text="{Binding Converter={StaticResource IntToIntTextOrDefaultConverter}}" VerticalAlignment="Center"/> 
         </WrapPanel> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </DataGridComboBoxColumn.EditingElementStyle> 
    </DataGridComboBoxColumn> 
</DataGrid.Columns> 
</DataGrid> 

視圖模型(視圖模型實現INotifyPropertyChanged和的SetValue引發的PropertyChanged):

public class SelectedObjectsViewModel : ViewModel 
{ 
    private int[] _lineWidths = { -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
    public ObservableCollection<int> LineWidths { get; private set; }; 

    private ObservableCollection<SelectionSetViewModel> _selectionSets; 
    public ObservableCollection<SelectionSetViewModel> SelectionSets 
    { 
     get { return _selectionSets; } 
     set { this.SetValue(ref _selectionSets, value); } 
    } 

    private SelectionSetViewModel _selectedSelectionSet; 
    public SelectionSetViewModel SelectedSelectionSet 
    { 
     get { return this._selectedSelectionSet; } 
     set { this.SetValue(ref _selectedSelectionSet, value); } 
    } 
} 

視圖模型爲DataGrid行(視圖模型實現INotifyPropertyChanged和的SetValue引發的PropertyChanged):

public class SelectionSetViewModel : ViewModel 
{ 
    public SelectionSetViewModel() 
    { 
     LineWidthIndex = -1; 
    } 
    private int _lineWidthIndex; 
    public int LineWidthIndex 
    { 
     get { return _lineWidthIndex; } 
     set { SetValue(ref _lineWidthIndex, value); } 
    } 

轉換器:

public class IntToIntTextOrDefaultConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     if ((int)value == -1) 
      return Fusion.App.Current.Resources["StrDefault"].ToString(); 

     return value.ToString(); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     return value.Equals(true) ? parameter : Binding.DoNothing; 
    } 
} 
+0

幸運的可能是,綁定需要Mode = TwoWay? –

+0

當然,但Web上的示例都沒有DataGridComboBoxColumn綁定定義中的Mode = TwoWay。我試圖用兩種不同的方式,但沒有奏效。此外,文本列的工作模式= TwoWay。如果你有一個想法如何正確地把它放在XAML我會很高興:) –

+0

由於我以前也使用複選框綁定,這裏是我自己的代碼的一部分:''。改了名字,但希望這有助於。 –

回答

1

似乎在某些情況下,例如在編輯文本列並按下回車或添加新行th之後e屬性WAS在更改組合框值後實際更新(set called)。所以我只是添加UpdateSourceTrigger = PropertyChanged綁定和更新源屬性立即發生(而不是一些隨機操作後)。請注意,從ComboBox更改焦點不足以更新源屬性,所以我認爲它從未更新過。

<DataGrid Name="SelectionSetsGrid" CanUserAddRows="False" CanUserResizeColumns="True" CanUserSortColumns="True" 
       ItemsSource="{Binding SelectionSets}" AutoGenerateColumns="False" 
       SelectedItem="{Binding SelectedSelectionSet}"> 
     <DataGridComboBoxColumn Header="{StaticResource XpStrTopologyWidth}" SelectedItemBinding="{Binding LineWidthIndex, UpdateSourceTrigger=PropertyChanged}"> 
      <DataGridComboBoxColumn.ElementStyle> 
       <Style TargetType="ComboBox" BasedOn="{StaticResource Theme.ComboBox.Style}"> 
        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.LineWidths}"/> 
        <Setter Property="IsReadOnly" Value="True"/> 
        <Setter Property="ItemTemplate"> 
         <Setter.Value> 
          <DataTemplate> 
           <WrapPanel> 
            <TextBlock Text="{Binding Converter={StaticResource IntToIntTextOrDefaultConverter}}" VerticalAlignment="Center"/> 
           </WrapPanel> 
          </DataTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </DataGridComboBoxColumn.ElementStyle> 
      <DataGridComboBoxColumn.EditingElementStyle> 
       <Style TargetType="ComboBox" BasedOn="{StaticResource Theme.ComboBox.Style}"> 
        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.LineWidths}"/> 
        <Setter Property="ItemTemplate"> 
         <Setter.Value> 
          <DataTemplate> 
           <WrapPanel> 
            <TextBlock Text="{Binding Converter={StaticResource IntToIntTextOrDefaultConverter}}" VerticalAlignment="Center"/> 
           </WrapPanel> 
          </DataTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </DataGridComboBoxColumn.EditingElementStyle> 
     </DataGridComboBoxColumn>       
    </DataGrid.Columns> 
</DataGrid> 
相關問題