您的位置:首页 > 健康 > 美食 > 平凉市政府门户网站_哈尔滨短视频运营_杭州百度优化_谁能给我个网址

平凉市政府门户网站_哈尔滨短视频运营_杭州百度优化_谁能给我个网址

2025/4/18 8:21:58 来源:https://blog.csdn.net/qq_42789677/article/details/146958017  浏览:    关键词:平凉市政府门户网站_哈尔滨短视频运营_杭州百度优化_谁能给我个网址
平凉市政府门户网站_哈尔滨短视频运营_杭州百度优化_谁能给我个网址

在绑定数据变化时手动触发UI状态检查

<Window x:Class="WpfApp5.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:WpfApp5"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.DataContext><local:MainViewModel /></Window.DataContext><StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"><Button Content="点击"  Command="{Binding ClickCommand}"  Margin="10" Padding="15" /><Button Content="Sub"  Command="{Binding ClickCommandSub}"  Margin="10" Padding="15" /><TextBlock Text="{Binding Count, StringFormat='已点击次数: {0}'}"  HorizontalAlignment="Center" Width="80" Margin="10"/></StackPanel>
</Window>
public partial class MainWindow : Window
{public MainWindow(){InitializeComponent();DataContext = new MainViewModel();}
}public class ManualRelayCommand : ICommand
{private readonly Action<object> _execute;private readonly Func<object, bool> _canExecute;public ManualRelayCommand(Action<object> execute, Func<object, bool> canExecute = null){_execute = execute;_canExecute = canExecute;}public bool CanExecute(object parameter) => _canExecute?.Invoke(parameter) ?? true;public void Execute(object parameter) => _execute(parameter);// 手动触发事件  public event EventHandler CanExecuteChanged;public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);//public event EventHandler CanExecuteChanged//{//    add => CommandManager.RequerySuggested += value;//    remove => CommandManager.RequerySuggested -= value;//} 同时保留手动触发方法  //public void ForceRaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
}public class MainViewModel : INotifyPropertyChanged
{public event PropertyChangedEventHandler PropertyChanged;protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));private int _count;public int Count{get => _count;set{if (_count == value) return;_count = value;OnPropertyChanged();// 手动触发命令状态更新  ClickCommand.RaiseCanExecuteChanged();}}public ManualRelayCommand ClickCommand { get; }public ICommand ClickCommandSub{get { return new ManualRelayCommand(_ => Count--, _ => true); }set { }}public MainViewModel(){ClickCommand = new ManualRelayCommand(ClickEvent,_CanExecute );}private void ClickEvent(object obj){Count++;}private bool _CanExecute(object arg){return Count < 3;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com