复选框像单选按钮 wpf c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17320456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:05:19  来源:igfitidea点击:

checkbox like radiobutton wpf c#

c#wpfxamlwpf-controlswpftoolkit

提问by rockenpeace

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:

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

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

my xaml.cs:

我的 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");
            }

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

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

回答by BaconSah

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 part is just a stack panel called: Name="checkboxcontainer"

XAML 部分只是一个名为:Name="checkboxcontainer" 的堆栈面板

Codebehind part:

代码隐藏部分:

    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;
            }
        }
    }