Skip to main content
 首页 » 编程设计

c#之如何将子用户控件中的按钮链接到父级。 C#/WPF

2024年02月23日16EasonJim

我有一个 WPF 应用程序,它也使用我设计的自定义控件。 在这个自定义控件中,我有一些按钮,我想在父窗口中执行一些操作。

我该怎么做? 谢谢!

请您参考如下方法:

您需要将按钮的命令属性公开为依赖属性。
假设您有一个自定义控件(与用户控件不同),定义如下:

<Style TargetType="{x:Type custom:MyButtonedCtrl}"> 
        <Setter Property="Template"> 
            <Setter.Value> 
                <ControlTemplate TargetType="{x:Type custom:MyButtonedCtrl}"> 
                    <Border BorderThickness="4" 
                            CornerRadius="2" 
                            BorderBrush="Black"> 
                        <StackPanel> 
                            <Button Command="{TemplateBinding CommandForFirstButton}"/> 
                            <Button Command="{TemplateBinding CommandForSecondButton}"/> 
                        </StackPanel> 
                    </Border> 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
    </Style> 

然后在后面的代码中,您必须公开 2 个依赖属性:CommandForFirstButton 和 CommandForSecondButton(类型为 ICommand):

public class MyButtonedCtrl : ContentControl 
    { 
        static MyButtonedCtrl() 
        { 
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButtonedCtrl), new FrameworkPropertyMetadata(typeof(MyButtonedCtrl)));       
        } 
 
        #region CommandForFirstButton 
        public ICommand CommandForFirstButton 
        { 
            get { return (ICommand)GetValue(CommandForFirstButtonProperty); } 
            set { SetValue(CommandForFirstButtonProperty, value); } 
        } 
 
        // Using a DependencyProperty as the backing store for CommandForFirstButton.  This enables animation, styling, binding, etc... 
        public static readonly DependencyProperty CommandForFirstButtonProperty = 
            DependencyProperty.Register("CommandForFirstButton", typeof(ICommand), typeof(MyButtonedCtrl), new UIPropertyMetadata(null)); 
        #endregion 
 
        #region CommandForSecondButton 
        public ICommand CommandForSecondButton 
        { 
            get { return (ICommand)GetValue(CommandForSecondButtonProperty); } 
            set { SetValue(CommandForSecondButtonProperty, value); } 
        } 
 
        // Using a DependencyProperty as the backing store for CommandForSecondButton.  This enables animation, styling, binding, etc... 
        public static readonly DependencyProperty CommandForSecondButtonProperty = 
            DependencyProperty.Register("CommandForSecondButton", typeof(ICommand), typeof(MyButtonedCtrl), new UIPropertyMetadata(null)); 
        #endregion 
    } 

每当你想使用你的控件时:

        <custom:MyButtonedCtrl CommandForFirstButton="{Binding MyCommand}"  
                               CommandForSecondButton="{Binding MyOtherCommand}"/> 

编辑:对于用户控件:

声明如下:

<UserControl x:Class="MyApp.Infrastructure.CustomControls.MyButtonedCtrl" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Name="buttonedCtrl"> 
    <Grid> 
        <Border BorderThickness="4" 
                            CornerRadius="2" 
                            BorderBrush="Black"> 
            <StackPanel> 
                <Button Command="{Binding CommandForFirstButton, ElementName=buttonedCtrl}"/> 
                <Button Command="{Binding CommandForSecondButton, ElementName=buttonedCtrl}"/> 
            </StackPanel> 
        </Border> 
    </Grid> 
</UserControl> 

代码隐藏是:

/// <summary> 
    /// Interaction logic for MyButtonedCtrl.xaml 
    /// </summary> 
    public partial class MyButtonedCtrl : UserControl 
    { 
        public MyButtonedCtrl() 
        { 
            InitializeComponent(); 
        } 
 
        #region CommandForFirstButton 
        public ICommand CommandForFirstButton 
        { 
            get { return (ICommand)GetValue(CommandForFirstButtonProperty); } 
            set { SetValue(CommandForFirstButtonProperty, value); } 
        } 
 
        // Using a DependencyProperty as the backing store for CommandForFirstButton.  This enables animation, styling, binding, etc... 
        public static readonly DependencyProperty CommandForFirstButtonProperty = 
            DependencyProperty.Register("CommandForFirstButton", typeof(ICommand), typeof(MyButtonedCtrl), new UIPropertyMetadata(null)); 
        #endregion 
 
        #region CommandForSecondButton 
        public ICommand CommandForSecondButton 
        { 
            get { return (ICommand)GetValue(CommandForSecondButtonProperty); } 
            set { SetValue(CommandForSecondButtonProperty, value); } 
        } 
 
        // Using a DependencyProperty as the backing store for CommandForSecondButton.  This enables animation, styling, binding, etc... 
        public static readonly DependencyProperty CommandForSecondButtonProperty = 
            DependencyProperty.Register("CommandForSecondButton", typeof(ICommand), typeof(MyButtonedCtrl), new UIPropertyMetadata(null)); 
        #endregion 
    } 

并且您以相同的方式使用它。

希望这对您有所帮助!