【问题标题】:Button Command Binding按钮命令绑定
【发布时间】:2013-10-09 00:13:07
【问题描述】:

我在列表框中有一个按钮。 我想将命令绑定到主网格的 DataContext。 我不确定谁来做,下面是我的尝试。

我想绑定到我的视图模型上的 ViewModel.SelectionEditorSelectionSelectedCommand,主网格绑定到,我不想绑定到实际的 filteredSelection.SelectionEditorSelectionSelectedCommand

这是我的 XAML

<Grid Name="MainGrid">
   .....
    <ListBox x:Name="MarketsListBox"  Height="Auto" MaxHeight="80" ItemsSource="{Binding Path=FilteredMarkets}" Margin="5" Width="Auto" HorizontalAlignment="Stretch"
                 >
          ListBox.ItemTemplate>
                    <DataTemplate>
                        <WrapPanel Orientation="Horizontal">
                            <Button Height="Auto" 
                                    Content="{Binding FinishingPosition,Converter={StaticResource FinishingPositionToShortStringConverter1}}" 
                                    Foreground="{Binding Path=FinishingPosition, Converter={StaticResource FinishingPositionToColourConverter1}}" 
                                    Margin="2" Width="20"
                                    Command="{Binding ElementName=MainGrid.DataContext, Path=SelectionEditorSelectionSelectedCommand}" 
                                    CommandParameter="{Binding}"
                                    />
    .....

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

    使用ElementName 绑定到网格应该可以,但是您在绑定语法中犯了一个小错误。 ElementName 必须包含名称​​仅,而不是属性。您只需在Path 中包含DataContext

    Command="{Binding ElementName=MainGrid,
                      Path=DataContext.SelectionEditorSelectionSelectedCommand}"
    

    【讨论】:

    • 我会试试看我的进展情况
    【解决方案2】:

    所以基于这一行:

    Command="{Binding ElementName=MainGrid.DataContext ... }
    

    我假设你有这样的东西:

    <Grid Name="MainGrid">
        <Grid.DataContext>
            <lol:GridViewModel /> <!--Some kind of view model of sorts-->
        </Grid.DataContext>
        ... content
    </Grid>
    

    那么您所要做的就是在 ViewModel 类上创建一个返回某种ICommand 的公共属性,例如:

    class GridViewModel {
        public ICommand SelectionEditorSelectionSelectedCommand { 
            get { return new TestCommand(); } 
        }
    }
    

    TestCommand 将是某种实现ICommand 的类,如:

    class TestCommand : ICommand {
        public event EventHandler CanExecuteChanged { get; set; }
    
        public bool CanExecute(object parameter)
        {
            return true; // Expresses whether the command is operable or disabled.
        }
    
        public void Execute(object parameter)
        {
             // The code to execute here when the command fires.
        }
    }
    

    基本上,对于ICommand,您只需要定义命令Executes 时会发生什么,如何确定它是否CanExecute,然后为CanExecuteChanged 提供事件句柄。一旦你完成了这个设置,你所要做的就是像这样连接你的按钮:

    <Button Command="{Binding SelectionEditorSelectionSelectedCommand}" />
    

    就是这样。基本上,绑定会自动检查您的 ViewModel 类是否有一个名为 SelectionEditorSelectionSelectedCommand 的属性,该属性实现了 ICommand。当它读取属性时,它将实例化TestCommand 的一个实例,WPF 将从那里处理它。当点击按钮时,Execute 会像发条一样被触发。

    【讨论】:

      【解决方案3】:

      你应该像我在类似情况下一样尝试:

      <Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid}}, Path=DataContext.YOURCOMMANDHERE}" />
      

      我在 TabItem Header 中有一个按钮,它工作正常! 问题是,你的 Command 是 DataContext 的一个属性,所以你的路径应该指明它。

      祝你好运!

      编辑:Elementname 也可以工作。

      【讨论】:

        猜你喜欢
        • 2019-05-11
        • 1970-01-01
        • 2013-09-16
        • 2016-06-25
        • 2015-06-07
        • 2013-06-07
        • 1970-01-01
        • 1970-01-01
        • 2016-11-22
        相关资源
        最近更新 更多