获取组中选定的单选按钮 (WPF)

Get Selected Radio Button in a Group (WPF)(获取组中选定的单选按钮 (WPF))

本文介绍了获取组中选定的单选按钮 (WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个 ItemsControl,其中包含单选按钮列表.

<ItemsControl ItemsSource="{Binding Insertions}"><ItemsControl.ItemTemplate><数据模板><网格><RadioButton GroupName="插入"/></网格></数据模板></ItemsControl.ItemTemplate></ItemsControl>

如何以MVVM的方式在group Insertions中找到选中的单选按钮?

我在互联网上找到的大多数示例都涉及在转换器的帮助下设置您将 IsChecked 属性绑定到的各个布尔属性.

我可以绑定到 ListBox SelectedItem 的等效项吗?

解决方案

想到的一个解决方案是向插入实体添加一个 IsChecked 布尔属性并将其绑定到 `IsChecked' 属性的单选按钮.这样您就可以在 View Model 中选中Checked"单选按钮.

这是一个快速而肮脏的例子.

注意:我忽略了 IsChecked 也可以为 null 的事实,如果需要,您可以使用 bool? 来处理.p>

简单的视图模型

使用系统;使用 System.Collections.Generic;使用 System.Collections.ObjectModel;命名空间 WpfRadioButtonListControlTest{类 MainViewModel{public ObservableCollection<插入>插入{得到;放;}公共 MainViewModel(){插入 = 新的 ObservableCollection<插入>();Insertions.Add(new Insertion() { Text = "Item 1" });Insertions.Add(new Insertion() { Text = "Item 2", IsChecked=true });Insertions.Add(new Insertion() { Text = "Item 3" });Insertions.Add(new Insertion() { Text = "Item 4" });}}类插入{公共字符串文本 { 获取;放;}公共布尔 IsChecked { 获取;放;}}}

XAML - 后面的代码没有显示,因为除了生成的代码之外没有其他代码.

<Window x:Class="WpfRadioButtonListControlTest.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-命名空间:WpfRadioButtonListControlTest"标题=主窗口"高度=350"宽度=525"><窗口.资源><local:MainViewModel x:Key="ViewModel"/></Window.Resources><Grid DataContext="{StaticResource ViewModel}"><ItemsControl ItemsSource="{绑定插入}"><ItemsControl.ItemTemplate><数据模板><网格><RadioButton GroupName="插入"内容="{绑定文本}"IsChecked="{绑定 IsChecked, Mode=TwoWay}"/></网格></数据模板></ItemsControl.ItemTemplate></ItemsControl></网格></窗口>

I have a ItemsControl in my program that contains a list of radio buttons.

<ItemsControl ItemsSource="{Binding Insertions}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <RadioButton GroupName="Insertions"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

How do I find the selected radio button in the group Insertions in a MVVM manner?

Most of the examples I have found on the internet involve setting individual boolean properties that you bind the IsChecked property to with the help of a converter.

Is there an equivalent of the ListBox SelectedItem that I can bind to?

解决方案

One solution that comes to mind is to add an IsChecked boolean property to your Insertion entities and bind that to the `IsChecked' property of the Radio button. This way you can check the 'Checked' radio button in View Model.

Here is a quick and dirty example.

NB: I ignored the fact that the IsChecked can also be null, you could handle that using bool? if required.

The simple ViewModel

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace WpfRadioButtonListControlTest
{
  class MainViewModel
  {
    public ObservableCollection<Insertion> Insertions { get; set; }

    public MainViewModel()
    {
      Insertions = new ObservableCollection<Insertion>();
      Insertions.Add(new Insertion() { Text = "Item 1" });
      Insertions.Add(new Insertion() { Text = "Item 2", IsChecked=true });
      Insertions.Add(new Insertion() { Text = "Item 3" });
      Insertions.Add(new Insertion() { Text = "Item 4" });
    }
  }

  class Insertion
  {
    public string Text { get; set; }
    public bool IsChecked { get; set; }
  }
}

The XAML - The code behind is not shown since it has no code other than than the generated code.

<Window x:Class="WpfRadioButtonListControlTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfRadioButtonListControlTest"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <local:MainViewModel x:Key="ViewModel" />
  </Window.Resources>
  <Grid DataContext="{StaticResource ViewModel}">
    <ItemsControl ItemsSource="{Binding Insertions}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Grid>
            <RadioButton GroupName="Insertions" 
                         Content="{Binding Text}" 
                         IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
          </Grid>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</Window>

这篇关于获取组中选定的单选按钮 (WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:获取组中选定的单选按钮 (WPF)

基础教程推荐