如何在Radiobutton检查事件上禁用Wpf数据网格中的特定单元格

时间:2014-06-20 12:27:25

标签: c# wpf

我想在单选按钮检查事件的wpf中禁用Datagrid中的特定单元格。我得到了我需要禁用的单元格的行索引和列索引。

1 个答案:

答案 0 :(得分:0)

CS:

   public class InverseConverter : IValueConverter
   {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           bool _val = (bool)value;
           return !_val;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool _val = (bool)value;
            return !_val;
        }
   }


   public MainWindow()
   {
        InitializeComponent();
        this.DataContext = this;        
   }

   public List<object> Stuff
   {
        get { return new List<object> { 1, 2, 3 }; }
   } 

XAML:

<Window>
  <Grid>
     <Grid.Resources>
        <local:InverseConverter x:Key="converter" />
     </Grid.Resources>

     <DataGrid ItemsSource="{Binding Stuff}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <RadioButton IsChecked="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType=ContentPresenter}, Converter={StaticResource converter}, Mode=OneWayToSource}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>                                          
            </DataGridTemplateColumn>
        </DataGrid.Columns>

     </DataGrid>        
 </Grid>