c# - WPF 将按钮绑定(bind)到命令

标签 c# wpf xaml button data-binding

我的屏幕如下所示:

Screen Snapshot

每个彩色区域都是放入 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 中的按钮绑定(bind)到 MainContentVM 中的 ChangePageCommand?我所拥有的不起作用,调试时单击任一按钮时都不会调用 ChangePageCommand。

当我使用 Command="{Binding ChangePageCommand,relativeSource={RelativeSource AncestorType={x:Type local:NavigationView}}}" 时,不应将命令的绑定(bind)设置为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}}}" />

关于c# - WPF 将按钮绑定(bind)到命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18766604/

相关文章:

wpf - 关于分辨率与 DPI 的问题?

wpf - 在样式的 EventTrigger 中触发命令?

C# WPF datgird IvalueConverter 绑定(bind)问题

c# - 在运行时更改颜色查找纹理

c# - 在 ASP.NET MVC 中保留用户数据

wpf - 设置绑定(bind)到 WPF 用户控件内的自定义 DependencyProperty

c# - 使用 Linq 在 C# 中进行列表操作

c# - 当绑定(bind)属性为空时,WPF DataGrid 显示 'NULL'

c# - 如何将数组索引绑定(bind)到 DataGrid 行索引?

wpf - 用户控件中的用户控件 : cannot locate resource