【问题标题】:Binding IsReadOnly using IValueConverter使用 IValueConverter 绑定 IsReadOnly
【发布时间】:2013-11-15 15:10:04
【问题描述】:

也许我误解了如何使用 IValueConverter 或数据绑定(这很可能),但我目前正在尝试根据字符串的值设置 DataGridTextColumn 的 IsReadOnly 属性。这是 XAML:

<DataGridTextColumn Binding="{Binding Path=GroupDescription}" Header="Name"
                    IsReadOnly="{Binding Current,
                                 Converter={StaticResource currentConverter}}"/>

这是我的转换器:

public class CurrentConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = value as string;
        if (s == "Current")
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

目前,该列始终是可编辑的,转换器似乎什么都不做。有人对为什么会发生这种情况有一些想法吗?

【问题讨论】:

  • 问题/问题是什么?
  • 更新了问题的结尾,希望不会太含糊...
  • IsReadOnly 不可绑定。在stackoverflow.com/questions/18443063/… 上查看我的回答
  • @Shoe - IsReadOnly 可以像任何其他 DP 一样绑定。
  • @mastur - Current 的财产在哪里?在您的 ViewModel 类或底层 ItemsSource 对象中?

标签: c# wpf data-binding


【解决方案1】:

您可以使用DataTrigger 来启用\禁用DataGridCell,而不是使用转换器:

<DataGridTextColumn Header="Name" Binding="{Binding GroupDescription}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Current}" Value="Current">
                    <Setter Property="TextBlock.IsEnabled" Value="False" />                                    
                </DataTrigger>                              
            </Style.Triggers>
        </Style>                       
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

【讨论】:

    【解决方案2】:

    DataGridTextColumn 的 IsReadOnly 属性的值是一个全局值,它将影响所有单元格。单个单元格没有自己的 IsReadOnly 属性。 尝试像这样创建自己的 DependencyPropertyhttp://blog.spencen.com/2009/04/25/readonly-rows-and-cells-in-a-datagrid.aspx

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      相关资源
      最近更新 更多