Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ItemControl和数据绑定时,RadioButton分组在WPF中不起作用_C#_Wpf_Xaml_Data Binding_Radiobuttonlist - Fatal编程技术网

C# 使用ItemControl和数据绑定时,RadioButton分组在WPF中不起作用

C# 使用ItemControl和数据绑定时,RadioButton分组在WPF中不起作用,c#,wpf,xaml,data-binding,radiobuttonlist,C#,Wpf,Xaml,Data Binding,Radiobuttonlist,在使用WPF表单和数据绑定时,我无法对单选按钮进行分组 如果我手动创建控件,行为符合预期,用户只能从radiobutton组(底部GroupBox)中选择一个项目,但如果我使用嵌套的ItemControls生成布局并使用数据绑定,则分组无法按预期工作,并且允许用户在一个组(顶部GroupBox)中选择多个radiobutton从这个链接的屏幕截图中可以看到 我已尝试绑定GroupName属性,但找不到用于GroupName属性的绑定属性。 也许可以使用同一网格行中TextBlock的Text属性

在使用WPF表单和数据绑定时,我无法对单选按钮进行分组

如果我手动创建控件,行为符合预期,用户只能从radiobutton组(底部GroupBox)中选择一个项目,但如果我使用嵌套的ItemControls生成布局并使用数据绑定,则分组无法按预期工作,并且允许用户在一个组(顶部GroupBox)中选择多个radiobutton从这个链接的屏幕截图中可以看到

我已尝试绑定GroupName属性,但找不到用于GroupName属性的绑定属性。 也许可以使用同一网格行中TextBlock的Text属性,但我找不到正确的绑定语法来引用它

让分组工作起来的方法是什么

以下是XAML:

<Window x:Class="RadioButtonMadness.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Title="MainWindow" SizeToContent="WidthAndHeight">

    <Window.Resources>
        <Style TargetType="RadioButton">
            <Setter Property="Margin" Value="10" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Height" Value="22" />
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="10" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Height" Value="22" />
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions />
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <GroupBox Header="Prognostic factors - NOK">
            <ItemsControl ItemsSource="{Binding PrognosticFactors}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Name="PrognosticFactors" Grid.Row="0" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="2*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions />
                            <TextBlock Text="{Binding Title}" />
                            <ItemsControl ItemsSource="{Binding Options}" Grid.Column="1">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <RadioButton Content="{Binding Key}" />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </GroupBox>
        <GroupBox Header="Prognostic factors - OK" Grid.Row="1">
            <StackPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="2*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions/>
                    <TextBlock Text="Test4" Grid.Column="0"/>
                    <StackPanel Orientation="Horizontal" Grid.Column="1">
                        <RadioButton Content="One"/>
                        <RadioButton Content="Two"/>
                        <RadioButton Content="Three"/>
                    </StackPanel>
                </Grid>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="2*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions />
                    <TextBlock Text="Test5" Grid.Column="0"/>
                    <StackPanel Orientation="Horizontal" Grid.Column="1">
                        <RadioButton Content="One"/>
                        <RadioButton Content="Two"/>
                        <RadioButton Content="Three"/>
                    </StackPanel>
                </Grid>
            </StackPanel>
        </GroupBox>
    </Grid>
</Window>

以及背后的代码:

using System;
using System.Collections.Generic;
using System.Windows;

namespace RadioButtonMadness
{
    public class Option
    {
        public String Key{ get; set; }
        public Double Value { get; set; }

        public Option(String key, Double value)
        {
            Key = key;
            Value = value;
        }
    }

    public class PrognosticFactor
    {
        private String title;
        public List<Option> Options
        {
            get
            {
                var list = new List<Option>();
                list.Add(new Option("One", 1));
                list.Add(new Option("Two", 2));
                list.Add(new Option("Three", 3));
                return list;
            }
        }

        public String Title
        {
            get { return title; }
        }

        public PrognosticFactor(String value)
        {
            title = value;
        }
    }

    public class ViewModel
    {
        public List<PrognosticFactor> PrognosticFactors
        {
            get
            {
                var list = new List<PrognosticFactor>();
                list.Add(new PrognosticFactor("Test1"));
                list.Add(new PrognosticFactor("Test2"));
                list.Add(new PrognosticFactor("Test3"));
                return list;
            }
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Windows;
命名空间RadioButtonManness
{
公共类选项
{
公共字符串密钥{get;set;}
公共双值{get;set;}
公共选项(字符串键,双值)
{
钥匙=钥匙;
价值=价值;
}
}
公共类预测因子
{
私有字符串标题;
公共列表选项
{
得到
{
var list=新列表();
增加(新选项(“一”,1));
增加(新选项(“两个”,2));
增加(新选项(“三”,3));
退货清单;
}
}
公共字符串标题
{
获取{返回标题;}
}
公共预测因子(字符串值)
{
标题=价值;
}
}
公共类视图模型
{
公共列表预测因子
{
得到
{
var list=新列表();
添加(新预测因子(“Test1”);
添加(新的预测因子(“测试2”));
添加(新预测因子(“Test3”);
退货清单;
}
}
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=新的ViewModel();
}
}
}

只需将GroupName绑定到Title属性。您可以这样做:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <RadioButton Content="{Binding Key}" GroupName="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Path=DataContext.Title}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

只需将GroupName绑定到Title属性。您可以这样做:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <RadioButton Content="{Binding Key}" GroupName="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Path=DataContext.Title}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>


太棒了!完全正确我尝试绑定到ItemsControl而不是Grid。哇!杰出的完全正确我尝试绑定到ItemsControl而不是Grid。哇!