如果依赖属性为真,则 ICommand 属性应执行

Posted

技术标签:

【中文标题】如果依赖属性为真,则 ICommand 属性应执行【英文标题】:If dependency property is true, then ICommand property should execute 【发布时间】:2021-11-27 22:16:22 【问题描述】:

好的,我在不同的文件中有自定义控件及其样式和具有 ICommand 属性的视图模型。

CustomControl.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 DepenencyProperty 为 True 时,无论是否按下按钮,都应执行命令,即 ExecuteCommand(命令名称用于按钮下的 CustomButtonsStyle.xaml)。

现在命令与按钮完美绑定,当我按下按钮时,它工作正常。

但问题是,假设 CmdExec="True",那么无论是否按下按钮都无关紧要,命令应该完成它的工作。 我尝试在 CustomButton.cs 的 ValueChangeProperty 中执行此操作,但仍然无法实现。

任何帮助如何解决此问题,当 CmdExec 为 true 时,应执行 ExecuteCommand ICommand 属性。

【问题讨论】:

【参考方案1】:

我可能误解了你的解释。 但是,如果您在 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);
      
    

P.S. 没有完整的代码,但根据显示的 XAML,命令参数绑定最好这样声明:

      <Button x:Name="LoadButton"
              Grid.Row="1"
              Height="50"
              HorizontalAlignment="Stretch"
              Command="Binding ExecuteCommand"
              CommandParameter="TemplateBinding Critical"
              Content="CmndExec"
              IsEnabled="true" />

【讨论】:

命令参数不用担心,它只是为执行命令打开的对话框。它说是或否。就是这样。 @EldHasp 这类似于您的 XAML。但重点不同。当 CmdExec 从 false 转换为 true 时,ValuePropertyChanged 方法是否会触发?如果有效,该命令是否有效? 它会触发 CmdExec 但是,该命令不会自己执行,尽管当我单击按钮时它会执行。 这很可能是由于命令的逻辑。您能否详细描述触发 ValuePropertyChanged 方法及其进一步的分步 (F11) 执行时会发生什么?执行是否进入 if 块?是否调用了 Execute (self.Critical) 方法?然后是 ExecuteCommandMethod 方法?问题可能出在元素的初始化序列中。可能是在调用 ValuePropertyChanged 方法时数据上下文尚未设置。 你帮了大忙。我正在考虑如何处理执行命令时出现的命令参数。现在,它可以工作了

以上是关于如果依赖属性为真,则 ICommand 属性应执行的主要内容,如果未能解决你的问题,请参考以下文章