clamp
clamp

Reputation: 34026

How to link buttons in child user-control to parent. C# / WPF

i have a WPF application that also uses a custom control i designed. in this custom control i have some buttons which i would like to give some actions in the parent window.

how can i do that? thanks!

Upvotes: 1

Views: 1866

Answers (1)

Louis Kottmann
Louis Kottmann

Reputation: 16628

You need to expose the Buttons' Commands properties as dependency properties.
Say you have a Custom Control (which is DIFFERENT from a UserControl), defined like this:

<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>

Then in your code behind, you have to expose the 2 dependency properties: CommandForFirstButton and CommandForSecondButton (of type 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
    }

And whenever you want to use your control:

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

EDIT : For a UserControl:

Declared like this:

<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>

The code-behind would be:

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

And you use it the same way.

Hope this helps!

Upvotes: 4

Related Questions