c# - 如果依赖属性为真,则 ICommand 属性应执行
问题描述
好的,我在不同的文件中有自定义控件及其样式和具有 ICommand 属性的视图模型。
自定义控件.cs
public class CustomButtons: Control
{
public static readonly DependencyProperty CmdExecProperty =
DependencyProperty.Register(nameof(CmdExec), typeof(bool), typeof(CustomButtons),
new PropertyMetadata(false, ValuePropertyChange));
public bool CmdExec
{
get => (bool)GetValue(CmdExecProperty);
set => SetValue(CmdExecProperty, value);
}
private static void ValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is CustomButtons self)
{
DataViewModel dataViewModel = (DataViewModel)self.DataContext;
if (self.CmdExec)
{
dataViewModel.ExecuteCommand.Execute(dataViewModel.ExecuteCommand);
}
}
}
}
CustomButtonsStyle.xaml
</ResourceDictionary.MergedDictionaries>
<!-- Control template for a CustomButtons -->
<ControlTemplate x:Key="CustomButtonsTemplate"
TargetType="{x:Type v:CustomButtons}">
<Grid Width="128"
d:DesignHeight="200">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition MaxHeight="52" />
</Grid.RowDefinitions>
<Button x:Name="LoadButton"
Grid.Row="1"
Height="50"
HorizontalAlignment="Stretch"
Command="{Binding ExecuteCommand}"
CommandParameter="{Binding Path=Critical,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type v:CustomButtons}},
Mode=OneWay}"
Content="CmndExec"
IsEnabled="true" />
</Button>
</Grid>
</ControlTemplate>
<Style x:Key="CustomButtonsStyle"
TargetType="{x:Type v:CustomButtons}">
<Setter Property="Template" Value="{StaticResource CustomButtonsTemplate}" />
</Style>
<Style TargetType="{x:Type v:CustomButtons}" BasedOn="{StaticResource CustomButtonsStyle}" />
</ResourceDictionary>
DataViewModel.cs 命令在文件中。
private ICommand _executeCommand;
public ICommand ExecuteCommand
{
get
{
return _executeCommand
?? (_executeCommand = new DelegateCommand<string>(ExecuteCommandMethod));
}
}
用法
<kit:CustomButtons x:Name="Buttons"
CmdExec="True"/>
此 CustomControl 工作正常,但我希望当 CmdExec DepenpencyProperty 为 True 时,无论是否按下按钮,都应执行命令,即 ExecuteCommand(在 Button 下的 CustomButtonsStyle.xaml 中使用命令名)。
现在命令与按钮完美绑定,当我按下按钮时,它工作正常。
但问题是,假设 CmdExec="True",那么无论是否按下按钮都无关紧要,命令应该完成它的工作。我尝试在 CustomButton.cs 的 ValueChangeProperty 中执行此操作,但仍然无法实现。
任何帮助如何解决此问题,当 CmdExec 为 true 时,应执行 ExecuteCommand ICommand 属性。
解决方案
我可能误解了你的解释。但是,如果您在 XAML 中执行类似的命令,那么它应该是这样的:
private static void ValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is CustomButtons self &&
(bool) e.NewValue &&
self.DataContext is DataViewModel dataViewModel)
{
dataViewModel.ExecuteCommand.Execute(self.Critical);
}
}
PS没有完整的代码,但是根据显示的 XAML,命令参数绑定最好声明如下:
<Button x:Name="LoadButton"
Grid.Row="1"
Height="50"
HorizontalAlignment="Stretch"
Command="{Binding ExecuteCommand}"
CommandParameter="{TemplateBinding Critical}"
Content="CmndExec"
IsEnabled="true" />
推荐阅读
- javascript - firebase auth 不是一个函数
- scala - 如何用值“未定义”替换值“未知”?
- sql - Pentaho (spoon):为 SQL 中的多行生成主键
- reactjs - 什么是用于快照测试的 react-scripts Jest 默认序列化程序?
- android - Firebase-ui 错误
- asp.net-mvc - 如何使用带有局部视图的 ViewModel
- node.js - 将 jBPM 与 Node.js 和 PostgreSQL 集成
- c - 以递归方式打印具有其他列表(相同结构)元素的列表
- powershell - PowerShell 远程启动进程不起作用
- c# - 列表的扩展方法
无法识别 C# 类的实例方法