【问题标题】:WPF Binding Button to CommandWPF 绑定按钮到命令
【发布时间】:2013-09-16 23:28:08
【问题描述】:

我的屏幕如下所示:

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

<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 中的 Button 绑定到 MainContentVM 中的 ChangePageCommand?我所拥有的不起作用,调试时单击任一按钮时永远不会调用 ChangePageCommand。

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

【问题讨论】:

    标签: c# wpf xaml button data-binding


    【解决方案1】:

    试试这个:

     <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}}}" />
    

    【讨论】:

    • 更改为您建议的内容后,我的按钮现在为浅灰色,文本为中灰色,单击按钮仍不会调用 ChangePageCommand。
    • @BrianKE,进一步阅读您的ChangePageCommand,您不应该在您的命令绑定中提供一个参数(IPageViewModel)吗?由于p =&gt; p is IPageViewModel 返回False,按钮变灰,所以按钮被禁用。
    • 修复了它。一定是在尝试各种绑定解决方案时不小心删除了它。
    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2012-09-07
    • 2013-10-09
    相关资源
    最近更新 更多