I have to disable a some button.
我必须禁用一些按钮。
How I can use TextBox.Triggers for that?
我怎么能用TextBox.Triggers呢?
Are there any samples?
有样品吗?
Thank you for reply!
感谢您的回复!
3 个解决方案
#1
3
Let's say you have a TextBox
and a Button
, and you want to disable your Button
when TextBox
is empty. This can be easily achieved with DataTriggers
:
假设你有一个TextBox和一个Button,你想在TextBox为空时禁用你的Button。使用DataTriggers可以轻松实现这一点:
<TextBox x:Name="textBox" />
<Button>
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=textBox}" Value="">
<Setter Property="Button.IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
#2
4
I am assuming this is related to your other question about triggering the Enabled property on a Button based on if a TextBox has a validation error or not.
我假设这与你在Button上触发Enabled属性的其他问题相关,基于TextBox是否有验证错误。
If that's so, you would use a DataTrigger
to test the TextBox.Validation.HasError
property to see if it has any errors, and if so disable the Button
如果是这样,您将使用DataTrigger测试TextBox.Validation.HasError属性以查看它是否有任何错误,如果是,则禁用Button
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="True" />
<DataTrigger Binding="{Binding ElementName=MyTextBox, Path=Validation.HasError" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style>
Be sure you bind your TextBox with ValidatesOnDataErrors="True"
for this to work
确保将TextBox与ValidatesOnDataErrors =“True”绑定,以使其正常工作
<TextBox x:Name="MyTextBox" Text="{Binding SomeText, ValidatesOnDataErrors=True }" />
As a side note, my comment on your other question still applies here. I would personally implement IDataErrorInfo
in your ViewModel
and make the SaveCommand.CanExecute()
only return true if ViewModel.IsValid
. Then it will automatically take care of disabling the button if the SaveCommand
is not supposed to execute
作为旁注,我对您的其他问题的评论仍然适用于此。我个人会在ViewModel中实现IDataErrorInfo,并使SaveCommand.CanExecute()仅在ViewModel.IsValid时返回true。然后,如果不应该执行SaveCommand,它将自动关闭禁用按钮
#3
0
Although posting code sample would be helpful and would allow a much better solution, I can still recommend data binding. Something like
虽然发布代码示例会有所帮助并且可以提供更好的解决方案,但我仍然可以推荐数据绑定。就像是
<Button Name="btnFoo"
Enabled="{Binding ElementName=txtblkBar, Converter={StaticResource ButtonVisibilityConverter}"/>
where the resources section of your control contains
控件的resources部分包含的位置
<local:ButtonVisibilityConverter Name="ButtonVisibilityConverter"/>
and you have defined a class ButtonVisibilityConverter
in the namespace referenced by local. The Data Conversion section of the page I linked to above has an example converter class.
并且您已在local引用的命名空间中定义了一个ButtonVisibilityConverter类。我上面链接的页面的数据转换部分有一个示例转换器类。
EDIT:
编辑:
Code that sets the button to disabled whenever txtblkBar
is empty:
每当txtblkBar为空时将按钮设置为禁用的代码:
[ValueConversion(typeof(TextBlock), typeof(bool?))]
public class ButtonVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
TextBlock txtblk = value as TextBlock;
if (null == txtblk)
return false;
return !string.IsNullOrEmpty(txtblk.Text);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Don't need to use the back conversion
throw new NotImplementedException();
}
}
#1
3
Let's say you have a TextBox
and a Button
, and you want to disable your Button
when TextBox
is empty. This can be easily achieved with DataTriggers
:
假设你有一个TextBox和一个Button,你想在TextBox为空时禁用你的Button。使用DataTriggers可以轻松实现这一点:
<TextBox x:Name="textBox" />
<Button>
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=textBox}" Value="">
<Setter Property="Button.IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
#2
4
I am assuming this is related to your other question about triggering the Enabled property on a Button based on if a TextBox has a validation error or not.
我假设这与你在Button上触发Enabled属性的其他问题相关,基于TextBox是否有验证错误。
If that's so, you would use a DataTrigger
to test the TextBox.Validation.HasError
property to see if it has any errors, and if so disable the Button
如果是这样,您将使用DataTrigger测试TextBox.Validation.HasError属性以查看它是否有任何错误,如果是,则禁用Button
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="True" />
<DataTrigger Binding="{Binding ElementName=MyTextBox, Path=Validation.HasError" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style>
Be sure you bind your TextBox with ValidatesOnDataErrors="True"
for this to work
确保将TextBox与ValidatesOnDataErrors =“True”绑定,以使其正常工作
<TextBox x:Name="MyTextBox" Text="{Binding SomeText, ValidatesOnDataErrors=True }" />
As a side note, my comment on your other question still applies here. I would personally implement IDataErrorInfo
in your ViewModel
and make the SaveCommand.CanExecute()
only return true if ViewModel.IsValid
. Then it will automatically take care of disabling the button if the SaveCommand
is not supposed to execute
作为旁注,我对您的其他问题的评论仍然适用于此。我个人会在ViewModel中实现IDataErrorInfo,并使SaveCommand.CanExecute()仅在ViewModel.IsValid时返回true。然后,如果不应该执行SaveCommand,它将自动关闭禁用按钮
#3
0
Although posting code sample would be helpful and would allow a much better solution, I can still recommend data binding. Something like
虽然发布代码示例会有所帮助并且可以提供更好的解决方案,但我仍然可以推荐数据绑定。就像是
<Button Name="btnFoo"
Enabled="{Binding ElementName=txtblkBar, Converter={StaticResource ButtonVisibilityConverter}"/>
where the resources section of your control contains
控件的resources部分包含的位置
<local:ButtonVisibilityConverter Name="ButtonVisibilityConverter"/>
and you have defined a class ButtonVisibilityConverter
in the namespace referenced by local. The Data Conversion section of the page I linked to above has an example converter class.
并且您已在local引用的命名空间中定义了一个ButtonVisibilityConverter类。我上面链接的页面的数据转换部分有一个示例转换器类。
EDIT:
编辑:
Code that sets the button to disabled whenever txtblkBar
is empty:
每当txtblkBar为空时将按钮设置为禁用的代码:
[ValueConversion(typeof(TextBlock), typeof(bool?))]
public class ButtonVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
TextBlock txtblk = value as TextBlock;
if (null == txtblk)
return false;
return !string.IsNullOrEmpty(txtblk.Text);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Don't need to use the back conversion
throw new NotImplementedException();
}
}