我有一个WPF组合框,我绑定了一些东西.用户必须选择其中一个项目.如果他们没有选择任何内容,我想发出警告并让用户重新选择.
怎么办?
我正在考虑使用"选择"按钮.当用户没有选择任何内容时,我设置:
if (combobox.SelectedItem == null)
{
MessageBox.Show("Please select one");
//Here is the code to go back to selection
}
Run Code Online (Sandbox Code Playgroud)
这个要求有通用的解决方案吗?
提前致谢.
您可以创建一个ValidationRule的ComboBox SelectedItem,,那么你可以有UI表明,他们需要做的事情的用户.
例:
验证规则:
public class SelectionValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
return value == null
? new ValidationResult(false, "Please select one")
: new ValidationResult(true, null);
}
}
Run Code Online (Sandbox Code Playgroud)
组合框:
<ComboBox ItemsSource="{Binding Items}" >
<ComboBox.SelectedItem>
<Binding Path="SelectedItem">
<Binding.ValidationRules>
<local:SelectionValidationRule ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
这将概述为ComboBox红色

当然WPF,我们可以自定义所有内容,因此您可以ControlTemplate为失败添加一个Validation并将验证消息添加为ToolTip.
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication9"
Title="MainWindow" Height="132" Width="278" Name="UI">
<Window.Resources>
<!--If there is a validation error, show in tooltip-->
<Style TargetType="ComboBox" >
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
<!--Create a template to show if validation fails-->
<ControlTemplate x:Key="ErrorTemplate">
<DockPanel>
<Border BorderBrush="Red" BorderThickness="1" >
<AdornedElementPlaceholder/>
</Border>
<TextBlock Foreground="Red" FontSize="20" Text=" ! " />
</DockPanel>
</ControlTemplate>
</Window.Resources>
<Grid DataContext="{Binding ElementName=UI}">
<ComboBox ItemsSource="{Binding Items}" Margin="21,20,22,48" Validation.ErrorTemplate="{StaticResource ErrorTemplate}">
<ComboBox.SelectedItem>
<Binding Path="SelectedItem">
<Binding.ValidationRules>
<local:SelectionValidationRule ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedItem>
</ComboBox>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
结果:

您可以通过多种方式实现这一目标,但一种方法可能是:
您可以像这样实现类型转换器:
[ValueConversion(typeof(object), typeof(bool))]
public class NullToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value != null);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,你可以像这样使用它:
<local:NullToBoolConverter x:Key="nullToBoolConverter"/>
<Button IsEnabled="{Binding ElementName=nameOfCombobox, Path=SelectedItem, Converter={StaticResource nullToBoolConverter}}" Content="Select"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6073 次 |
| 最近记录: |