i have a datagrid that contains 3 DataGridTemplateColumns that hold radioButtons. now i want to control the "isEnabled" property of the radioButtons from a click event of a button in my code behind:
<DataGrid AutoGenerateColumns="False" Height="300" x:Name="dgEmps"
RowEditEnding="gridEmps_RowEditEnding" CellEditEnding="gridEmps_CellEditEnding"
FlowDirection="RightToLeft"
SelectionChanged="gridEmps_SelectionChanged">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Shift Manager">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton GroupName="{Binding EmployeeId}"
IsChecked="{Binding IsManager, Mode=TwoWay}" x:Name="dgrIsManager"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Waiter" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton GroupName="{Binding EmployeeId}"
IsChecked="{Binding IsWaiter, Mode=TwoWay}" x:Name="dgrIsWaiter"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Cook" IsReadOnly="True" x:Name="aaa">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton GroupName="{Binding EmployeeId}"
IsChecked="{Binding IsCook, Mode=TwoWay}" x:Name="dgrIsCook"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
the thing is that the "IsReadOnly" property doesn't work and i can't seem to access the radioButton properties from my code (not even by the x:name i declare in the xaml). i tried this: WPF Toolkit DataGrid SelectionChanged Getting Cell Value and many other tutorials/ blog posts but couldn't make it work. any ideas?
You cannot access RadioButton by x:Name because it is defined in a DataTemplate, not in a main content tree.
Instead define DataTemplateColumns like this (based on your last column):
And then from code behind set aaa.IsReadOnly to true or false. This change should be propagated to every DataGridCell that belongs to this DataGridColumn, which will then change RadioButton.IsEnabled because of the binding.
XAML snippet uses BooleanOppositeConverter which is just a converter that inverts boolean values.
To make sure your readiobutton is ReadOnly is to set IsHitTestVisible