【问题标题】:I have a gridview in wpf and have a two radio buttons and a button in template column. how do i access the status of radio buttons?我在 wpf 中有一个 gridview,并且在模板列中有两个单选按钮和一个按钮。如何访问单选按钮的状态?
【发布时间】:2012-02-14 00:00:24
【问题描述】:

我在 wpf 中有一个 gridview,在模板列中有两个单选按钮和一个按钮。如何在按钮的单击事件中访问单选按钮的状态?

 <DataGrid>
     <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding TrackingID}" Header="TrackingID" Visibility="Hidden" Width="50" />
        <DataGridTextColumn Binding="{Binding UserFullName}" Header="Name" Width="140" />                                       
        <DataGridTemplateColumn Width="350">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
                            <RadioButton Width="50" Content="Yes" GroupName="status" />
                            <RadioButton Width="50" Content="No" GroupName="status" IsChecked="True" />
                            <Button Width="100" Click="Button_Click">View Details</Button>
                        </StackPanel>
                    </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
     </DataGrid.Columns>
 </DataGrid>

我正在为 Button_Click() 函数中的按钮捕获事件。在此功能中,我想知道选择了哪个单选按钮。

private void Button_Click(object sender, RoutedEventArgs e)
        {                
                int trackingid = Convert.ToInt32((((FrameworkElement)sender).DataContext as DataRowView)[0].ToString()); //get the tracking id of the request

                //(((FrameworkElement)sender).DataContext as RadioButton) //not working

        }

请帮忙。

【问题讨论】:

  • @Dmitry 我们没有使用 MVVM。
  • 对你有好处:) 然后我会使用 ElementName 的绑定。

标签: c# wpf vb.net xaml datagrid


【解决方案1】:

如果您不想使用绑定(如果您的“是/否”纯粹是 UI 属性或出于任何原因,您仍然可以选择。使用 x:Name - smth 为您的 RadioButtons 命名。就像“ YesRadioButton" 和 "NoRadioButton" 然后:

为共享容器(RadioButtons 的父级)调用 FinName("YesRadioButton")

使用ElementName绑定,即将你的按钮标签绑定到你的任何RadiButtons的IsChecked

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
 <RadioButton x:Name="YesRadioButton" Width="50" Content="Yes" GroupName="status" />
 <RadioButton x:Name="NoRadioButton" Width="50" Content="No" GroupName="status" IsChecked="True" />
 <Button Width="100" Click="Button_Click" Tag="{Binding ElementName='YesRadioButton', Path='IsChecked'}">View Details</Button>   
</StackPanel>

然后在您的按钮中单击只需检查发件人的Tag 属性。

【讨论】:

  • 你是对的!这就是我一直在寻找的。我从来没有想过我们可以使用 ElementName 绑定。谢谢:)
【解决方案2】:

在您的点击处理程序中,发送者是按钮。因此,您可以查找父 StackPanel,然后在子中搜索 RadioButtons,并查看哪个 IsChecked 为 true。

但是,根据 UI 项的确切设置,这是非常脆弱的。

更好的方法是将 IsChecked 绑定到模型上的属性(或绑定到 DataGridTextColumns 中的任何内容)。这样,如果您的视图发生变化,则不需要调整 Click-handler。

【讨论】:

  • @Dmitry 您的 DataGridTextColumns 中有一些要绑定的内容。
  • 是的,但它可以是一个本身没有“UI”属性的对象。
  • @Dmitry 该对象是您的模型。如果你发现你需要一些额外的属性来处理特定于视图的东西,你可以在两者之间添加一个层。该层称为 ViewModel。
  • @Daniel Rose 是吗?我当然知道,我想对你说的是,如果没有适当的 VM 层,你唯一的选择就是用 UI 属性污染你的 M,你最好不要这样做。
  • @Dmitry 是的,这可能会发生。那么问题是为什么没有完成添加 ViewModel 层。恕我直言,这是一个比尝试搜索命名的 UI 项更好的解决方案。
【解决方案3】:

Bind 将您需要的所有内容添加到您的项目中,然后将 Button(在 sender 中)的 DataContext 转换为该项目,并检查设置了哪些属性。

【讨论】:

  • 你是在说这个吗:(((FrameworkElement)sender).DataContext as RadioButton ?
  • @HotTester: 不,var data = (sender as FrameworkElement).DataContext as MyClassRadioButton.IsChecked 应该绑定到MyClass 的某个属性,然后你就可以得到那个属性。
猜你喜欢
  • 2021-01-06
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 2023-02-03
  • 1970-01-01
相关资源
最近更新 更多