我把我能想出的最简单的例子放在下面来证明我的问题。我正在尝试根据 2 个条件中的 1 个启用按钮 1) Textbox1 可见且内容有效或 2) Textbox2 可见且内容有效
我似乎正在根据可见性启用按钮,但 IsValid 方面让我很伤心。
对于按钮,我引入了 MultiDataTrigger 和 MultiBinding 以及 MultiBinding Converter 方法来评估是否应该启用按钮。当我在编辑框之间切换(通过单击单选按钮)时调用该方法(称为 myConverter),但在编辑框中的数据有效、无效或两者之间转换时似乎不会被调用。很可能,我没有正确处理 Validation.HasError
我的具体问题:1)处理这个问题的正确模式是什么?有什么例子吗?我应该说我已经简化了问题。例如,验证可能不仅仅是“必须是八个字符”,并且可能涉及多个编辑框(如“地址”和“zip”或“地址”和“状态”。因此我想我可能需要MultiBinding Converter 的想法,但我对其他想法持开放态度!2)如何在我的 Converter 方法中处理 Validation.HasError?我将其视为 ReadOnlyCollection,这可能是完全错误的!3)我认为我的大部分问题是由于处理错误信息的许多选择。鉴于我正在使用 ValidationRules,我是否还应该从支持编辑字段的属性中抛出异常?他们会被召唤吗?你能推荐一篇文章来展示不同的验证方法吗?
我已将所有代码放在下面。如果有人可以快速浏览并指出我正确的方向,我将不胜感激。-戴夫 XAML 代码
<Window x:Class="StackOverFlowBindingExample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StackOverFlowBindingExample"
Title="Window1" Height="Auto" MinWidth="500" SizeToContent="Manual" WindowStartupLocation="CenterOwner" ResizeMode="CanResizeWithGrip" >
<Window.Resources>
<local:MyConverter x:Key="myConverter" />
<Style x:Key="textStyleTextBox" TargetType="TextBox">
<Setter Property="Foreground" Value="#333333" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="MinHeight" Value="2" />
<Setter Property="MinWidth" Value="100" />
<Setter Property="Margin" Value="4" />
<Setter Property="MaxLength" Value="23" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Left" />
<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>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<RadioButton Name="m_radio1" Margin="4" GroupName="IdInputType" IsChecked="True" Checked="IdInputType_Changed">Use Inputtype1</RadioButton>
<RadioButton Name="m_radio2" Margin="4" GroupName="IdInputType" IsChecked="False" Checked="IdInputType_Changed">Use Inputtype2</RadioButton>
</StackPanel>
<DockPanel Name="Grp8Digit">
<Label MinHeight="25" Margin="4" VerticalAlignment="Top" VerticalContentAlignment="Center" HorizontalAlignment="Left" MinWidth="100" Width="113">Type 1 Id:</Label>
<TextBox Height="23" Name="m_textBox8DigitId" MaxLength="8" Width="120" Style="{StaticResource textStyleTextBox}" Validation.Error="TextBox_Error">
<TextBox.Text>
<Binding>
<Binding.ValidatesOnExceptions>true</Binding.ValidatesOnExceptions>
<Binding.ValidatesOnDataErrors>true</Binding.ValidatesOnDataErrors>
<Binding.UpdateSourceTrigger>PropertyChanged</Binding.UpdateSourceTrigger>
<Binding.Path>EightDigitId</Binding.Path>
<Binding.NotifyOnValidationError>true</Binding.NotifyOnValidationError>
<Binding.ValidationRules>
<local:EightByteStringConvertRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label MinHeight="25" Margin="4" VerticalAlignment="Top" VerticalContentAlignment="Center" HorizontalAlignment="Left" MinWidth="100">Enter 8 digit id</Label>
</DockPanel>
<DockPanel Name="Grp5Digit" Visibility="Collapsed">
<StackPanel Orientation="Horizontal">
<Label MinHeight="25" Margin="4" VerticalAlignment="Top" VerticalContentAlignment="Center" HorizontalAlignment="Left" MinWidth="100" Width="113">Type 2 id:</Label>
<TextBox Name="m_textBox5DigitId" Style="{StaticResource textStyleTextBox}" MinHeight="25" Margin="4" VerticalAlignment="Top" MaxLength="23" VerticalContentAlignment="Center" HorizontalAlignment="Left" MinWidth="100" Width="100" ToolTip="Enter Type 2 id">
<TextBox.Text>
<Binding>
<Binding.ValidatesOnExceptions>true</Binding.ValidatesOnExceptions>
<Binding.Path>FiveDigitId</Binding.Path>
<Binding.NotifyOnValidationError>true</Binding.NotifyOnValidationError>
<Binding.ValidationRules>
<local:FiveByteStringConvertRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label MinHeight="25" Margin="4" VerticalAlignment="Top" VerticalContentAlignment="Center" HorizontalAlignment="Left" MinWidth="100">Enter 5 digit id</Label>
</StackPanel>
</DockPanel>
</StackPanel>
<Button Height="27" Name="btnDoSomething" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="4" HorizontalContentAlignment="Center" Click="btnDoSomething_Click" Content="Do Something">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="false" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Value="true">
<Condition.Binding>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding ElementName="Grp8Digit" Path="Visibility" />
<Binding ElementName="m_textBox8DigitId" Path="Validation.HasError" />
<Binding ElementName="Grp5Digit" Path="Visibility" />
<Binding ElementName="m_textBox5DigitId" Path="Validation.HasError" />
</MultiBinding>
</Condition.Binding>
</Condition>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="true" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</Grid>
C# 代码
using System;
// lots of usings!!!
namespace StackOverFlowBindingExample
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window, INotifyPropertyChanged
{
private static readonly object eightDigitLock = new object();
private string _eightdigitId;
public string EightDigitId
{
get
{
return _eightdigitId;
}
set
{
lock (eightDigitLock)
{
if (value != _eightdigitId)
{
if (value.Length == 8)
_eightdigitId = value;
else
throw new Exception("Must be 8 digits");// do I really need to throw Exception here?
}
}
}
}
private static readonly object fiveDigitLock = new object();
private string _fivedigitId;
public string FiveDigitId
{
get
{
return _fivedigitId;
}
set
{
lock (fiveDigitLock)
{
if (value != _fivedigitId)
{
if (value.Length == 5)
_fivedigitId = value;
else
throw new Exception("Must be 5 digits");// do I really need to throw exception?
}
}
}
}
public Window1()
{
InitializeComponent();
this.DataContext = this;
}
private void IdInputType_Changed(object sender, RoutedEventArgs e)
{
if (m_radio1 != null && Grp8Digit != null && Grp5Digit != null)
{
if (m_radio1.IsChecked == true)
{
Grp8Digit.Visibility = Visibility.Visible;
Grp5Digit.Visibility = Visibility.Collapsed;
}
else
{
Grp8Digit.Visibility = Visibility.Collapsed;
Grp5Digit.Visibility = Visibility.Visible;
}
}
}
private void TextBox_Error(object sender, ValidationErrorEventArgs e)
{
try
{
if (e.Action == ValidationErrorEventAction.Added)
{
try
{
if (e.Error.Exception != null && e.Error.Exception.InnerException != null && e.Error.Exception.InnerException.Message.Length > 0)
{
((Control)sender).ToolTip = e.Error.Exception.InnerException.Message;
}
else
{
((Control)sender).ToolTip = e.Error.ErrorContent.ToString();
}
}
catch (Exception ex)
{
string msg = ex.Message;
//Common.ProgramContext.Current.AddSessionLogEntrySync(new LogEntry(LogEntryCategory.Exception, ex.ToString()));
((Control)sender).ToolTip = e.Error.ErrorContent.ToString();
}
}
else
{
((Control)sender).ToolTip = "";
}
}
catch (Exception)
{
//Common.ProgramContext.Current.AddSessionLogEntrySync(new LogEntry(LogEntryCategory.Exception, ex.ToString()));
((Control)sender).ToolTip = "";
}
}
private void btnDoSomething_Click(object sender, RoutedEventArgs e)
{
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
bool valid8Digit = true;
ReadOnlyCollection<ValidationError> collection = values[1] as ReadOnlyCollection<ValidationError>;
if (collection != null && collection.Count > 0)
{
valid8Digit = false;
}
//if ((bool)values[0] == true)//&& (bool)values[1] == false)
if ((Visibility)values[0] == Visibility.Visible && valid8Digit)
{
return true;
}
else
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class FiveByteStringConvertRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if ((value as string) != null && (value as string).Length == 5)
return new ValidationResult(true, "");
else
return new ValidationResult(false, "");
}
}
public class EightByteStringConvertRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if ((value as string) != null && (value as string).Length == 8)
return new ValidationResult(true, "");
else
return new ValidationResult(false, "");
}
}
}