繁体   English   中英

WPF绑定到命令按钮

[英]WPF Binding Button to Command

我有一个看起来像这样的屏幕:

屏幕快照

每个彩色区域是放入MainView.xaml中的不同UserControl,如下所示:

<UserControl Name="TopDockPanel" DockPanel.Dock="Top" Content="{Binding QuickInfoVM, Source={StaticResource Locator}}" />
<UserControl Name="LeftDockPanel" DockPanel.Dock="Left" Content="{Binding NavigationVM, Source={StaticResource Locator}}" />
<UserControl Name="RightDockPanel" DockPanel.Dock="Right" Content="{Binding MainContentVM, Source={StaticResource Locator}}" />

(我使用的是MVVMLight,因此Locator是ViewModelLocator)

绿色区域是ItemsControl区域,显示每个项目(公司和员工)的按钮:

<UserControl x:Class="MvvmLightDemo.View.NavigationView"
             Name="Navigation"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MvvmLightDemo.View"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="120"
             DataContext="{Binding MainContentVM, Source={StaticResource Locator}}" >

    <StackPanel Background="CadetBlue">
        <ItemsControl ItemsSource="{Binding PageViewModels}">
            <ItemsControl.ItemTemplate>

                <ItemContainerTemplate>
                    <Button Content="{Binding Name}" 
                            Style="{StaticResource NavigationTextStyle}" 
                            Command="{Binding ChangePageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:NavigationView}}}" />


                </ItemContainerTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

MainContentVM包含ChangePageCommand,如下所示:

        public RelayCommand<IPageViewModel> ChangePageCommand
        {
            get
            {
                return _changePageCommand = new RelayCommand<IPageViewModel>(
                        p => ChangeViewModel((IPageViewModel)p),
                        p => p is IPageViewModel);

            }
        }

我的问题是,如何将ItemContainerTemplate中的按钮绑定到MainContentVM中的ChangePageCommand? 我所拥有的不起作用,在调试ChangePageCommand时,单击任何一个按钮都不会调用。

当我使用Command="{Binding ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type local:NavigationView}}}"是否应将Command的绑定设置为NavigationView的绑定(DataContext = MainContentVM)?

尝试这个:

 <Button Content="{Binding Name}" 
                            Style="{StaticResource NavigationTextStyle}" 
                            Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:NavigationView}}}" />

更新:

 <Button Content="{Binding Name}" 
                                Style="{StaticResource NavigationTextStyle}" 
                                CommandParameter="{Binding}"
                                Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:NavigationView}}}" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM