制作一个按钮IsEnabled的状态取决于两个文本框 [英] Making a Button IsEnabled state depend on two textboxes
问题描述
我想有一个按钮的IsEnabled仅是真实的,如果两个文本框没有任何错误(在这种情况下,我只希望他们都为数字)
我试过文本框设置为NotifyOnValidationError = TRUE,以为这将提高该冒泡到容器控件例外 - 我会再设置按钮的IsEnabled为true基础上,Validation.HasError附加属性属于该控制
我尝试了以下(剥离的格式code),但它不工作(使能当我第一次启动应用程序的按钮和文本输入框是空的 - 他们必然要为空的小数)
< WrapPanel中>
<文本框X:名称=txtDeltaMin文本={绑定路径= DeltaMinFilter,NotifyOnValidationError = TRUE,ValidatesOnDataErrors = TRUE,ValidatesOnExceptions = TRUE}>< /文本框>
<文本框X:名称=txtDeltaMax文本={绑定路径= DeltaMaxFilter,NotifyOnValidationError = TRUE,ValidatesOnDataErrors = TRUE,ValidatesOnExceptions = TRUE}>< /文本框>
<按钮X:名称=btnUpdateFilter点击=btnUpdateFilter_Click的IsEnabled ={绑定的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType = {X:类型WrapPanel中}},路径=(Validation.HasError)}过滤器过滤&LT ; /按钮>
< / WrapPanel中>
您可以使用的 MultiBinding 。看起来应该是这样的:
<文本框文本={绑定路径= DeltaMinFilter,NotifyOnValidationError = TRUE,ValidatesOnDataErrors = TRUE,ValidatesOnExceptions = TRUE}/>
<文本框X:名称=txtDeltaMax文本={绑定路径= DeltaMaxFilter,NotifyOnValidationError = TRUE,ValidatesOnDataErrors = TRUE,ValidatesOnExceptions = TRUE}/>
<按钮X:名称=btnUpdateFilter点击=btnUpdateFilter_ClickCONTENT =过滤器>
< Button.IsEnabled>
< MultiBinding转换器={的StaticResource IsEnabledConverter}>
<绑定的ElementName =txtDeltaMin路径=Validation.HasError/>
<绑定的ElementName =txtDeltaMax路径=Validation.HasError/>
< / MultiBinding>
< /Button.IsEnabled>
< /按钮>
然后你只需要做的 IsEnabledConverter 的通过实施的 IMultiValueConverter 的,并将其添加为一个资源。类
公共类IsEnabledConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
的foreach(中值VAR的isValid)
如果(IsValid的是布尔?==假)
返回false;
返回true;
}
公共对象[] ConvertBack(对象的值,类型[] targetTypes,对象参数,System.Globalization.CultureInfo文化)
{
抛出新的NotImplementedException();
}
}
I want to have a button's IsEnabled to only be true if two textboxes don't have any errors (in this case, I just want them to both be numeric)
I tried setting the textboxes to NotifyOnValidationError=True, thinking this will raise an exception that bubbles up to a container control - I would then set the Button's IsEnabled to true based on the Validation.HasError attached property belonging to that control
I tried the following (stripped of formatting code) but it's not working (the button is enabled when I first start the application and the textboxes are empty - they are bound to nullable decimals)
<WrapPanel>
<TextBox x:Name="txtDeltaMin"Text="{Binding Path=DeltaMinFilter, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"></TextBox>
<TextBox x:Name="txtDeltaMax" Text="{Binding Path=DeltaMaxFilter, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"></TextBox>
<Button x:Name="btnUpdateFilter" Click="btnUpdateFilter_Click" IsEnabled="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type WrapPanel}}, Path=(Validation.HasError)}">Filter</Button>
</WrapPanel>
You can use a MultiBinding. Should look something like this:
<TextBox Text="{Binding Path=DeltaMinFilter, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
<TextBox x:Name="txtDeltaMax" Text="{Binding Path=DeltaMaxFilter, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
<Button x:Name="btnUpdateFilter" Click="btnUpdateFilter_Click" Content="Filter">
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource IsEnabledConverter}">
<Binding ElementName="txtDeltaMin" Path="Validation.HasError" />
<Binding ElementName="txtDeltaMax" Path="Validation.HasError" />
</MultiBinding>
</Button.IsEnabled>
</Button>
Then you just need to make the IsEnabledConverter class by implementing IMultiValueConverter, and add it as a resource.
public class IsEnabledConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
foreach (var isValid in values)
if (isValid as bool? == false)
return false;
return true;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这篇关于制作一个按钮IsEnabled的状态取决于两个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!