我有一个文本框
<TextBox x:Name="searchTextBox" Width="200" Height="30" HorizontalAlignment="Left"/>
和两个按钮
<Button x:Name="previous" Style="{StaticResource AppBarButtonStyle}" Tapped="OnOptionItemTapped" IsEnabled="False">
<Image Source="/Assets/images/left_arrow.png"/>
</Button>
<Button x:Name="next" Style="{StaticResource AppBarButtonStyle}" Tapped="OnOptionItemTapped" IsEnabled="False">
<Image Source="/Assets/images/right_arrow.png"/>
</Button>
是否有通过文本框启用/禁用按钮的简单解决方案?
例如,如果 TextBox 为空,则按钮将被禁用。如果 TextBox 不为空,则启用按钮。
最佳答案
您可以应用文本更改事件,在每次更改时检查输入。
<TextBox x:Name="searchTextBox" Width="200" Height="30" HorizontalAlignment="Left" TextChanged="TextBox_TextChanged" />
如果文本符合您的需要,您可以启用/禁用按钮。
public void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (searchTextBox.Text == result)
next.IsEnabled = false;
}
编辑:除了类似这种方法的代码隐藏之外,您还可以了解 MVVM 设计模式。其他答案部分使用了 MVVM 中常见的做法。
网络上有各种很好的教程。
关于c# - 根据 xaml 中的文本框禁用/启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51740336/