推迟 RadioButton 更改
Postpone RadioButton change
我正在寻找阻止切换 RadioButton
的可能性,但仍然捕获 Click
事件。不幸的是,使用 Enabled=false
或 IsHitTestVisible=false
属性会阻止 Click
事件。
我要实现的是:
1. 用户点击 RadioButton
。
2. 从 Click
事件调用一些方法,处理程序作为参数传递,但活动 RadioButton
尚未更改。
3.当调用处理程序时,根据结果我要切换RadioButton
或不切换。
您应该处理单选按钮上的 MouseDown 事件,然后它会阻止向下隧道将单选按钮设置为选中状态。
static void OnMouseDown(object sender, MouseButtonEventArgs e)
{
// if some logic...
e.Handled = true;
}
使用绑定,您可以将调用放在 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;
}
我为您创建了一个简单的示例。
不要忘记从 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
而不是事件处理程序。
希望对你有所帮助)
我正在寻找阻止切换 RadioButton
的可能性,但仍然捕获 Click
事件。不幸的是,使用 Enabled=false
或 IsHitTestVisible=false
属性会阻止 Click
事件。
我要实现的是:
1. 用户点击 RadioButton
。
2. 从 Click
事件调用一些方法,处理程序作为参数传递,但活动 RadioButton
尚未更改。
3.当调用处理程序时,根据结果我要切换RadioButton
或不切换。
您应该处理单选按钮上的 MouseDown 事件,然后它会阻止向下隧道将单选按钮设置为选中状态。
static void OnMouseDown(object sender, MouseButtonEventArgs e)
{
// if some logic...
e.Handled = true;
}
使用绑定,您可以将调用放在 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;
}
我为您创建了一个简单的示例。
不要忘记从 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
而不是事件处理程序。
希望对你有所帮助)