1

我正在将 wpf 与 mvvm 一起使用,并且我有一个包含 4 列的数据网格:ID、原始、已翻译、IsTranslated 当翻译列中的文本发生变化时我想要,IsTranslated 列的值自动更改为检查这是我的数据网格 xaml 代码:

<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> 
4

3 回答 3

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");
            }
        }
于 2013-01-20T15:41:54.960 回答
0

您应该将 IsTranslated 列绑定到 Translated 绑定路径,而不是 IsTranslated 绑定路径。

于 2013-01-20T15:02:07.577 回答
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;
        }
}
于 2013-01-20T15:35:10.683 回答