wpf - 如何根据wpf中其他控件的属性设置控件的属性

标签 wpf xaml

我有一个场景,根据文本框的文本值,我必须禁用和启用按钮说, 对于 TextBox.Text="abc"或 "cdf"应该禁用该按钮,而对于其他值应该启用它。

这只能用 Xaml 编写。

提前致谢

最佳答案

看起来您可以使用触发器来执行此操作:

当在文本框中输入值 ABC 时按钮被禁用,然后当值更改为 ABC 以外的值时启用。

<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Window.Resources>
    <Style x:Key="disableButton" TargetType="{x:Type Button}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=textBox1,Path=Text}" Value="ABC">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>

        </Style.Triggers>
    </Style>
</Window.Resources>

<StackPanel>
    <TextBox x:Name="textBox1"/>
    <Button Style="{StaticResource disableButton}" Height="23" Name="button1" Width="75">Button</Button>
</StackPanel>

关于wpf - 如何根据wpf中其他控件的属性设置控件的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1333756/

相关文章:

wpf - 问题绑定(bind) DataGridComboBoxColumn.ItemsSource

wpf - WPF中对MVVM的一句话解释?

c# - 绑定(bind)工作正常,但智能感知显示 : Cannot resolve property XXX in data context of type 'object'

wpf - 如何使滚动查看器在 WPF 中将高度设置为自动的情况下工作?

c# - 清除页面在 WP8 中使用的内存

.net - 在 WPF 应用程序中 native 拦截按键操作的方法

WPF 用百分比将 TextBlock 绑定(bind)到 ProgressBar

c# - CommandParameter 始终为空 wpf MVVM

xaml - 使用资源定义厚度

wpf - 沿路径对对象进行动画处理