2013-01-20 152 views
1

我使用wpf與mvvm和我有一個datagrid 4列: ID,原件,翻譯,IsTranslated 我想當翻譯列中的文本正在改變,IsTranslated列的值自動更改爲檢查 這是我的datagrid xaml代碼:如何在DataGridTextColumn值更改時更改DataGridCheckBoxColumn值?

<DataGrid Grid.Row="3" AutoGenerateColumns="False" ItemsSource="{Binding AllXliffs}"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="ID" Binding="{Binding Path=Id}" /> 
       <DataGridTextColumn Header="Original" Binding="{Binding Path=Original}" /> 
       <DataGridTextColumn Header="Translated" Binding="{Binding Path=Translated}" /> 
       <DataGridCheckBoxColumn Header="IsTranslated" Binding="{Binding Path=IsTranslated, Mode=TwoWay, Converter={StaticResource StringToBooleanConverter}, UpdateSourceTrigger=PropertyChanged}" ></DataGridCheckBoxColumn> 
      </DataGrid.Columns> 
</DataGrid> 

回答

1

由於您使用的MVVM,所以你可以在你的模型屬性增加一個小的商業邏輯:

像此:

 private string _translation; 
     private bool _isTranslated; 

     public string Translation 
     { 
      get { return _translation; } 
      set 
      { 
       _translation = value; 
       IsTranslated = !String.IsNullOrEmpty(_translation); 
      } 
     } 

     public bool IsTranslated 
     { 
      get { return _isTranslated; } 
      set 
      { 
       _isTranslated = value; 
       OnPropertyChanged("IsTranslated"); 
      } 
     } 
0

您應該將IsTranslated列綁定到已轉換的綁定路徑,而不是IsTranslated綁定路徑。

0

試試這個:

<Window.Resources> 
     <loca:StringToBooleanConverter x:Key="myCon" /> 
    </Window.Resources> 
    <Grid> 
     <DataGrid Grid.Row="3" AutoGenerateColumns="False" ItemsSource="{Binding}"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="ID" Binding="{Binding Path=Id}" /> 
       <DataGridTextColumn Header="Original" Binding="{Binding Path=Original}" /> 
       <DataGridTextColumn Header="Translated" Binding="{Binding Path=Translated}" /> 
       <DataGridCheckBoxColumn Header="IsTranslated" Binding="{Binding Path=Translated, Converter={StaticResource myCon}, UpdateSourceTrigger=PropertyChanged}" /> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 

轉換器應該是這樣的:

public class StringToBooleanConverter: IValueConverter 
{ 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      string translated = (string)value; 
      if (string.IsNullOrEmpty(translated)) 
       return false; 
      else 
       return true; 
     } 

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

他如何獲得IsTranslated數據以防止他想將其存儲到數據庫中? – HichemSeeSharp

+0

他沒有指定該值來自數據庫,他只想在文本更改時自動將此值更改爲true。 – kmatyaszek

+0

我的數據來自xml文件並存儲在其中。 –