【问题标题】:Disabling/Making read only all combobox in one column in datagrid禁用/使只读数据网格中一列中的所有组合框
【发布时间】:2013-10-11 07:07:38
【问题描述】:

我的网格已正确绑定,我所要做的就是根据后面代码中的任何条件禁用或使其只读 Column2 中包含的所有组合框。假设在渲染网格后,我们得到 10 行包含此组合框。我必须禁用所有这 10 行中的组合框列。

<DataGridTextColumn Binding="{Binding Value1}" Header="Column1" IsReadOnly="True"/>
    <DataGridTemplateColumn Header="Column2">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox SelectedItem="{Binding MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding MyComboItemSource}" >                                       
                </ComboBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGridTextColumn>

【问题讨论】:

    标签: wpf binding datagrid datagridcomboboxcolumn


    【解决方案1】:

    您可以为组合框中的 IsEnabled 属性使用转换器。

    类似

    <ComboBox IsEnabled ={Binding Path=XXXX, Converter = {StaticResource MyConverter}} .... />
    

    MyConverter 将检查您想要的属性并检索 false 或 true。 像这样的:

     public class MyConverter: IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if(value!=null)
    {
         if((int) value==1)
    return true;
    else return false;
    }
    
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    【讨论】:

      【解决方案2】:

      您只需要在 Code-Behind 中创建一个 bool 属性并绑定到 xaml 中组合框的 isEnabled 属性即可。

      代码隐藏

      private bool _Disable;
      
              public bool Disable
              {
                  get { return _Disable; }
                  set
                  {
                      _Disable= value;
                      OnPropertyChanged("Disable");
                  }
              }
      

      Xaml

      <ComboBox IsEnabled="{Binding Disable,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding MyComboItemSource}" >
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-11
        • 1970-01-01
        • 2016-09-30
        • 1970-01-01
        • 1970-01-01
        • 2013-08-06
        • 2018-06-27
        • 1970-01-01
        相关资源
        最近更新 更多