复选框像单选按钮 wpf c# [英] checkbox like radiobutton wpf c#

查看:30
本文介绍了复选框像单选按钮 wpf c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经调查了这个问题,但这在设计视图和代码隐藏中得到了解决.但我的问题几乎没有区别:我尝试仅使用代码隐藏来执行此操作,因为我的复选框是根据数据库数据动态创建的.换句话说,我的复选框数量不稳定.我只想选中一组复选框中的一个复选框.当我单击一个复选框时,我希望其他复选框的 ischecked 属性变为 false.这与单选按钮中的属性相同.我从 xaml 端的堆栈面板中取出我的复选框:

i have investigated this problem but this is solved in design view and code-behind. but my problem is little difference: i try to do this as only code-behind because my checkboxes are dynamically created according to database data.In other words, number of my checkboxes is not stable. i want to check only one checkbox in group of checkboxes. when i clicked one checkbox,i want that ischecked property of other checkboxes become false.this is same property in radiobuttons. i take my checkboxes from a stackpanel in xaml side:

<StackPanel Margin="4" Orientation="Vertical"  Grid.Row="1" Grid.Column="1" Name="companiesContainer">
            </StackPanel>

我的 xaml.cs:

my xaml.cs:

using (var c = new RSPDbContext())
        {
            var q = (from v in c.Companies select v).ToList();

            foreach (var na in q)
            {
                CheckBox ch = new CheckBox();
                ch.Content = na.Name;
                ch.Tag = na;
                companiesContainer.Children.Add(ch);
            }
        }

foreach (object i in companiesContainer.Children)
            {
                CheckBox chk = (CheckBox)i;

                chk.SetBinding(ToggleButton.IsCheckedProperty, "DataItem.IsChecked");
            }

如何在 xaml.cs 的复选框中提供此属性?提前致谢..

how can i provide this property in checkboxes in xaml.cs ? thanks in advance..

推荐答案

为选中的事件添加一个事件处理程序.创建复选框时,将此(相同)事件处理程序添加到每个复选框.

Add an an event handler for the checked event. When creating the checkbox, add this (same) event handler to each checkbox.

在事件处理程序中,运行您添加的每个复选框,对于每个复选框,取消选中它,除非它与发件人的复选框相同.

In the event handler, run through each checkbox you've added, and for every checkbox, uncheck it UNLESS it's the same checkbox as the sender.

我认为应该做到这一点(我的头顶).

That I think should do the trick (off the top of my head).

这是我刚刚敲出的一些代码,应该会有所帮助:

Here is some code I just knocked up that should help:

XAML 部分只是一个名为:Name="checkboxcontainer"

XAML part is just a stack panel called: Name="checkboxcontainer"

代码隐藏部分:

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        CreateCheckboxes();
    }

    private void CreateCheckboxes()
    {
        for (int i = 0; i <= 5; i++)
        {
            CheckBox c = new CheckBox();
            c.Name = "Check" + i.ToString();
            c.Checked += c_Checked; //This is adding the event handler to the checkbox
            checkboxcontainer.Children.Add(c);
        }
    }

    // This is the event handler for the checkbox
    private void c_Checked(object sender, RoutedEventArgs e) 
    {
        foreach (var control in checkboxcontainer.Children)
        {
            if (control is CheckBox && control != sender)
            {
                CheckBox cb = (CheckBox)control;
                cb.IsChecked = false;
            }
        }
    }

这篇关于复选框像单选按钮 wpf c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆