Get Selected Radio Button in a Group (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)
 
				
         
 
            
        基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				