推迟RadioButton改变

时间:2016-12-06 11:00:13

标签: c# wpf events mouseevent code-behind

我正在寻找阻止切换RadioButton的可能性,但仍然会捕获Click事件。不幸使用Enabled=falseIsHitTestVisible=false属性会阻止Click事件。

我想要达到的目的是:
1.用户点击RadioButton
2.从Click事件调用一些方法,并将处理程序作为参数传递,但活动RadioButton尚未更改。
3.调用handler时,取决于我想要切换RadioButton的结果。

3 个答案:

答案 0 :(得分:2)

您应该在单选按钮上处理MouseDown事件,然后它将阻止隧道向下将单选按钮设置为已选中。

static void OnMouseDown(object sender, MouseButtonEventArgs e)
{
     // if some logic...
     e.Handled = true;
}

答案 1 :(得分:1)

使用绑定可以将调用放在setter中,如下所示:

<强> XAML

<RadioButton Content="radiobutton" IsChecked="{Binding TestRadio, Mode=TwoWay}"/>

<强>码

    private bool _testRadio;
    public bool TestRadio
    {
        get { return _testRadio; }
        set { value = testradiohandler(); SetProperty(ref _testRadio, value); }
    }
    private bool testradiohandler()
    {
        return new Random().NextDouble() >= 0.5;
    }

答案 2 :(得分:1)

我为你创建了一个简单的例子。

不要忘记从NuGet Prism包中获取。

我创建了三个RadioButton并从某个ViewModel为Func<bool>设置了它们。在PreviewMouseDown事件触发后,我从Func<bool>属性调用当前委托,Tag

视图模型:

namespace PostponeRadioButtonChange.Model
{
    using System;
    using System.Collections.Generic;

    using Microsoft.Practices.Prism.Mvvm;

    public class MainWindow : BindableBase
    {
        private List<Func<bool>> rbHandlers;

        private string comment;

        public List<Func<bool>> RbHandlers
        {
            get { return this.rbHandlers; }
            private set { this.SetProperty(ref this.rbHandlers, value); }
        }

        public string Comment
        {
            get { return this.comment; }
            set { this.SetProperty(ref this.comment, value); }
        }

        public MainWindow()
        {
            this.RbHandlers = new List<Func<bool>>
            {
                () =>
                    {
                        this.Comment = "First RadioButton clicked";
                        return false;    // Here must be your condition for checking
                    },
                () =>
                    {
                        this.Comment = "Second RadioButton clicked";
                        return false;
                    },
                () =>
                    {
                        this.Comment = "Third RadioButton clicked";
                        return true;   // For example, third not checked after click
                    }
            };
        }
    }
}

观点内容(设计师);

<StackPanel>

    <TextBox Text="{Binding Path=Comment, Mode=OneWay}"/>

    <RadioButton Content="First"
                 PreviewMouseDown="RadioButtonMouseDown"
                 Tag="{Binding Path=RbHandlers[0], Mode=OneTime}"/>

    <RadioButton Content="Second"
                 PreviewMouseDown="RadioButtonMouseDown"
                 Tag="{Binding Path=RbHandlers[1], Mode=OneTime}"/>

    <RadioButton Content="Third"
                 PreviewMouseDown="RadioButtonMouseDown"
                 Tag="{Binding Path=RbHandlers[2], Mode=OneTime}"/>

</StackPanel>

查看(代码隐藏):

namespace PostponeRadioButtonChange
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    using VM = PostponeRadioButtonChange.Model;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new VM.MainWindow();
        }

        private void RadioButtonMouseDown(object sender, MouseButtonEventArgs e)
        {
            var rb = (sender as RadioButton);

            if (rb == null)
                throw new InvalidCastException("RadioButtonMouseDown only for RadioButton's");

            e.Handled = (rb.Tag as Func<bool>)?.Invoke() ?? false;
        }
    }
}

这对最终解决方案不利,但作为一个例子可以帮助你。您还可以在VM中创建Command而不是事件处理程序。

我希望,它会帮助你)