子控件中的绑定命令?

问题描述 投票:0回答:1
c# wpf
1个回答
0
投票

绑定错误表明问题出在 Button.ICommand 属性上:

要修复此问题,请在 WindowControlButtons 绑定中添加

ElementName=root
,以便绑定到声明的依赖项属性而不是 DataContext:

<UserControl x:Class="TombLib.WPF.Controls.WindowControlButtons"
             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:TombLib.WPF.Controls"
             mc:Ignorable="d"
             xmlns:darkUI="clr-namespace:DarkUI.WPF;assembly=DarkUI.WPF"
             xmlns:vm="clr-namespace:TombLib.WPF.ViewModels"
             xmlns:sg="clr-namespace:SpacedGridControl;assembly=SpacedGridControl"
             d:DesignHeight="100" d:DesignWidth="300"
             x:Name="root">
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Right" Height="Auto" Orientation="Horizontal">
        <Button Name="oKButton" ... Command="{Binding Close, ElementName=root}" CommandParameter="{Binding Window, ElementName=root}" Content="OK"/>
        <Button Name="cancelButton" ... Command="{Binding Path=Cancel, ElementName=root}" CommandParameter="{Binding Window, ElementName=root}" Content="Cancel"/>
    </StackPanel>
</UserControl>
© www.soinside.com 2019 - 2024. All rights reserved.