xamdatagrid Disable column conditionally
-
23-06-2021 - |
سؤال
I have a requirement of disabling a perticular cell in a row, depending on values in other cells of the same row.
WPF or WPF MVVM scenarios, both solutions are welcome.
I tried both and none works!!!
المحلول 2
Thanks @Grenter. I did solve it by implementing Converter class and implementing it as a static resource in my xaml file. Also, I figured out that we can do the same using the cell updating or cell updated event and writing some lines of code in the xaml code behind, which I generally donot prefer.
But converters is certainly the way to go forward.
Thanks a lot!!!
نصائح أخرى
If I have understood you correctly.
You might want to take a look at DataGridTemplateColumn within the Datagrid.Columns section (see Code Below) as this allows you to set up your own templates for the column/cell and then you can set the enabled property. The following is only a simple solution and going forward I would have it so that when you change the enable checkbox that updates the model which in turn would enable the items in the row.
<DataGrid Name="dgrgrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Name}" IsEnabled="{Binding Enabled}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Type" />
<DataGridTextColumn Header="Size" />
<DataGridTextColumn Header="Price" />
<DataGridCheckBoxColumn Header="Row Enabled" Binding="{Binding Enabled}" />
</DataGrid.Columns>
</DataGrid>
for the code behind I have a simple observable collection of a class called Cheese which has some properties that you see below. (This is basic M-V-VM technique)