本文介绍了从代码控制RadioButton DataGridTemplateColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格,其中包含3个保存单选按钮的DataGridTemplateColumns.现在我想通过后面的代码中按钮的click事件来控制radioButtons的"isEnabled"属性:

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>

问题是"IsReadOnly"属性不起作用,而且我似乎无法从我的代码访问radioButton属性(甚至不通过我在xaml中声明的x:name).我尝试了这个: WPF工具包数据网格选择更改了获取单元格值和其他许多教程/博客文章,但无法使其正常工作.有什么想法吗?

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 Valueand many other tutorials/ blog posts but couldn't make it work.any ideas?

推荐答案

您无法通过x:Name访问RadioButton,因为它是在DataTemplate中而不是在主内容树中定义的.

You cannot access RadioButton by x:Name because it is defined in a DataTemplate, not in a main content tree.

代替这样定义DataTemplateColumns(基于您的最后一列):

Instead define DataTemplateColumns like this (based on your last column):

<DataGridTemplateColumn Header="Cook" IsReadOnly="True" x:Name="aaa">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <RadioButton GroupName="{Binding EmployeeId}" 
                         IsChecked="{Binding IsCook, Mode=TwoWay}"  x:Name="dgrIsCook"
                         IsEnabled={Binding IsReadOnly, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Converter={StaticResource BooleanOppositeConverter}}/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

然后从后面的代码中将aaa.IsReadOnly设置为true或false.此更改应传播到属于此DataGridColumn的每个DataGridCell,然后由于绑定而将更改RadioButton.IsEnabled.

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代码段使用BooleanOppositeConverter,它只是一个可转换布尔值的转换器.

XAML snippet uses BooleanOppositeConverter which is just a converter that inverts boolean values.

这篇关于从代码控制RadioButton DataGridTemplateColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:08