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)
基础教程推荐
- 将数据集转换为列表 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 如果条件可以为空 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
