我正在繼續探索 WPF 并嘗試使用命令來遵循 MVVM。我的簡單代碼如下。
主視窗.xaml
<Window x:Class="ClickCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ClickCommand"
xmlns:vm="clr-namespace:ClickCommand.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<vm:ButtonViewModel x:Key="ViewModel"/>
</Window.Resources>
<Grid>
<StackPanel Width="350" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Vertical">
<Button Height="80" FontSize="32" Background="LightBlue" Content="Click Me"
Command="{Binding ButtonCommand, Source={StaticResource ViewModel}}"/>
<TextBox x:Name="textBoxTest" Margin="5" FontSize="36"/>
<Button Height="80" FontSize="32" Background="LightBlue" Content="Click if TextBox as Text"
Command="{Binding ButtonParameterCommand, Source={StaticResource ViewModel}}" CommandParameter="{Binding Text, ElementName=textBoxTest}"/>
</StackPanel>
</Grid>
</Window>
ButtonViewModel.cs
public ButtonViewModel()
{
ButtonCommand = new ButtonCommand();
ButtonParameterCommand = new ButtonParameterCommand();
}
public ICommand ButtonCommand { get; }
public ICommand ButtonParameterCommand { get; }
命令庫檔案
abstract class CommandBase : ICommand
{
public event EventHandler CanExecuteChanged;
public virtual bool CanExecute(object parameter)
{
return true;
}
public abstract void Execute(object parameter);
protected void OnCanExecutedChanged()
{
CanExecuteChanged?.Invoke(this, new EventArgs());
}
}
按鈕命令.cs
public override void Execute(object parameter)
{
MessageBox.Show("Button is Clicked");
}
ButtonParameterCommand.cs
public override bool CanExecute(object parameter)
{
return true;
}
public override void Execute(object parameter)
{
MessageBox.Show(parameter as string);
}
我的問題是,如果 IsNullOrEmpty 上方的文本框我想禁用第二個按鈕,但是我認為可以解決此問題的代碼會使按鈕永久禁用。我一直在嘗試的代碼是`
public override bool CanExecute(object parameter)
{
if (parameter != null)
{
if (String.IsNullOrEmpty(parameter as string))
{
return false;
}
else
{
return true;
}
}
return false;
}
這是我的錯誤還是 Xaml 系結?我希望有人可以提供幫助,并提前感謝您的幫助。`
uj5u.com熱心網友回復:
CanExecuteChanged每當文本TextBox更改時,您都需要引發命令事件。
創建類的OnCanExecutedChanged()方法并從系結到的源屬性的 setter 中呼叫它:CommandBasepublicTextBox
public class ButtonViewModel
{
public ButtonViewModel()
{
ButtonCommand = new ButtonCommand();
ButtonParameterCommand = new ButtonParameterCommand();
}
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
ButtonParameterCommand.OnCanExecutedChanged();
}
}
public ICommand ButtonCommand { get; }
public CommandBase ButtonParameterCommand { get; }
}
XAML:
<TextBox x:Name="textBoxTest" Margin="5" FontSize="36"
Text="{Binding Text, Source={StaticResource ViewModel}}"/>
但是您應該真正研究如何實作接受 anAction<object>和 a的“中繼”或“委托”命令,并分別Predicate<object>在Execute和CanExecute方法的實作中呼叫它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376984.html
下一篇:文本框部分只讀
