Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am facing an issue with disabling all cells in a column having value "ONE", I am able to read the string from datagrid but am unable to make that particaular cell disabled. Please let me know if there is a solution for it.

I am having entire row and column info where i am having the values, but am unable to disable that exact cell.

Thanks in advance

What I have tried:

I tried to get through the rows or columns as we do in datagridview was not working here.
Posted
Updated 1-Feb-17 4:52am
Comments
[no name] 1-Feb-17 8:41am    
"s we do in datagridview was not working here", of course not. WPF is not the same as Winforms. You get the cell by walking the visual tree until you find the cell you are looking for, then disable it.
Pete O'Hanlon 1-Feb-17 9:52am    
What do you mean by disabled? Are you trying to disable a TextBox in your DataGrid?
VamsiPenta 1-Feb-17 10:21am    
yes, i am trying to disable checkbox in the cell
Pete O'Hanlon 1-Feb-17 10:30am    
You have a checkbox with the value "One" behind it? Is the checkbox being controlled by another cell in the same row?
VamsiPenta 1-Feb-17 10:44am    
yes, it was based on another column in that same row

1 solution

Okay, here's how you do this. Suppose you have the following model:
C#
public class Data
{
  public bool IsChecked { get; set; }
  public string Value { get; set; }
}
Now, this is exposed to the DataGrid in a ViewModel like this:
C#
public ObservableCollection<Data> Items { get; } = new ObservableCollection<Data>();

private void Populate()
{
  Items.Add(new Data() { IsChecked = true, Value = "One" });
  Items.Add(new Data() { IsChecked = true, Value = "One" });
  Items.Add(new Data() { IsChecked = true, Value = "Two" });
  Items.Add(new Data() { IsChecked = true, Value = "One" });
  Items.Add(new Data() { IsChecked = true, Value = "Three" });
  Items.Add(new Data() { IsChecked = true, Value = "One" });
  Items.Add(new Data() { IsChecked = true, Value = "One" });
}
That's all very straightforward. Now, the final piece of the puzzle is the way we create our DataGrid. Specifically, we're going to create a CheckBoxStyle style that we use as our ElementStyle for the DataGridCheckBoxColumn and that style is going to have a DataTrigger that reacts to the contents of our Value property in our Data model. So, we end up with this XAML
XML
<DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
  <DataGrid.Resources>
    <Style TargetType="{x:Type CheckBox}" x:Key="CheckBoxStyle">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Value}" Value="One">
          <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </DataGrid.Resources>
  <DataGrid.Columns>
    <DataGridCheckBoxColumn Binding="{Binding IsChecked}" ElementStyle="{StaticResource CheckBoxStyle}" />
    <DataGridTextColumn Binding="{Binding Value}" />
  </DataGrid.Columns>
</DataGrid>
It's that simple.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900