如何根据值列表启用/禁用数据网格列单元格?

时间:2015-11-13 05:42:10

标签: wpf isenabled datagridcolumn

<DataGridTextColumn Header="Amount($)" Width="*" Binding="{Binding Path = Amount}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Tran_Code}" Value=**"DEP"**>
                                <Setter Property="IsEnabled" Value="False"/>                                 
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

不是只有一个值(截至现在是DEP),而是应为某些值启用单元格

2 个答案:

答案 0 :(得分:0)

您可以使用转换器:

<DataGridTextColumn Header="Amount($)" Width="*" Binding="{Binding Path = Amount}" Visibility="Binding Path = Tran_Code, Converter={StaticResource VisibilityConverter}"></DataGridTextColumn>

public class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string transcode = value.ToString();
                if (transcode == 1)
                    return Visibility.Hidden;                
                else
                    return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

答案 1 :(得分:0)

this.mydatagrid.Columns [2] .Visibility = Visibility.Hidden;

您可以添加要隐藏的列的索引。