【问题标题】:How to pass a delegate type parameter to the WPF command如何将委托类型参数传递给 WPF 命令
【发布时间】:2020-05-06 08:06:20
【问题描述】:

下午好! 我有以下查看代码(部分):

<dxg:TreeListControl x:Name="TlcPropertyGroups" ItemsSource="{Binding CatalogPropertyGroups}"  >
        <dxg:TreeListControl.Columns>
            <dxg:TreeListColumn FieldName="Name"  />
        </dxg:TreeListControl.Columns>
        <dxg:TreeListControl.View>
            <dxg:TreeListView x:Name="TlvPropertyGroups" ShowCheckboxes="True" AllowRecursiveNodeChecking="True"  ShowColumnHeaders="True" TreeDerivationMode="ChildNodesSelector" ChildNodesPath="CatalogPropertyGroupDetails" ShowIndicator="False">
                <dxg:TreeListView.HeaderTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <dxe:TextEdit EditMode="InplaceInactive" Text="Группы свойств"/>
                            <dxe:HyperlinkEdit Grid.Column="1" Text="Настроить" HorizontalAlignment="Right" Command="{Binding SettingCatalogPropertyGroupsCommand}"  DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dx:DXWindow}}}"/>
                        </Grid>
                    </DataTemplate>
                </dxg:TreeListView.HeaderTemplate>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Loaded">
                        <i:InvokeCommandAction Command="{Binding CmdTreeListViewLoaded}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </dxg:TreeListView>
        </dxg:TreeListControl.View>
    </dxg:TreeListControl>

有如下命令:

 private RelayCommand cmdTreeListViewLoaded;

    public RelayCommand CmdTreeListViewLoaded
    {
        get { return cmdTreeListViewLoaded ?? (cmdTreeListViewLoaded = new RelayCommand(obj =>
        {

            //some code


        })); }
    }

问题:如何将委托作为参数传递给命令(以隐藏一些表示级逻辑)? P.S.:例如,委托可能会显示一个简单的 MessageBox。

【问题讨论】:

  • 这篇文章在某种程度上没有提供信息!
  • 例如,我在视图中描述了一个委托: public Action ActionMethod = str => { MessageBox.Show(str); };如何在命令参数中指定?

标签: wpf xaml


【解决方案1】:

试试这个

<i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">

                     <i:InvokeCommandAction Command="{Binding CmdTreeListViewLoaded}"
                                           CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TreeListView}}" />
                </i:EventTrigger>
    </i:Interaction.Triggers>

【讨论】:

    【解决方案2】:

    解决办法如下:

    部分视图代码:

     public static Action<string> ActionMethod { get; set; } = str =>
     {
            MessageBox.Show(str);
     };
    

    部分 XAML 代码:

     <i:Interaction.Triggers>
             <i:EventTrigger EventName="Loaded">
                 <i:InvokeCommandAction Command="{Binding CmdTreeListViewLoaded}" CommandParameter="{x:Static tabPanels:PropertyGroupsPanel.ActionMethod}" />
             </i:EventTrigger>
      </i:Interaction.Triggers>
    

    部分 ViewModel 代码:

     private RelayCommand<Action<string>> cmdTreeListViewLoaded;
    
        public RelayCommand<Action<string>> CmdTreeListViewLoaded
        {
            get
            {
                return cmdTreeListViewLoaded ?? (cmdTreeListViewLoaded =
                           new RelayCommand<Action<string>>(obj =>
                           {
                               obj.Invoke("Тест!!!");
                           }));
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 2015-07-07
      • 2021-10-23
      • 2016-09-03
      相关资源
      最近更新 更多