Venom
Venom

Reputation: 155

wpf UserControl command button click binding

I have usercontrol:

<UserControl x:Class="MyApp.Header"
             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"
             mc:Ignorable="d" 
             d:DesignHeight="40" d:DesignWidth="300" DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}">

   <Grid>
        <Label Content="{Binding LableContent, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"></Label>
        <Button Command="{Binding Path=AddClick, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}">
            <Image Source="{StaticResource addImage}" Height="20"/>
        </Button>
    </Grid>
</UserControl>

And dependency property in usercontoror:

public string LableContent
{
    get { return (string)GetValue(LableContentProperty); }
    set { SetValue(LableContentProperty, value); }
}
public static readonly DependencyProperty LableContentProperty =
    DependencyProperty.Register("LableContent", typeof(string), typeof(Header));

public ICommand AddClick
{
    get { return (ICommand)GetValue(AddClickProperty); }
    set { SetValue(AddClickProperty, value); }
}
public static readonly DependencyProperty AddClickProperty =
            DependencyProperty.Register("AddClick", typeof(ICommand), typeof(Header));

I added usercontrol on mainwindow:

<local:Header AddClick="{Binding Path=AddUser_Click}" LableContent="Users"></local:Header>

And add click event on MainWindow.cs

private void AddUser_Click(object sender, RoutedEventArgs e)
{

}

The problem is that the Lable is being filled, but the command click the button is not called. What am I doing wrong?

Upvotes: 0

Views: 779

Answers (1)

Shivani Katukota
Shivani Katukota

Reputation: 859

There are two things you need to set

  1. Specify the DataContext for window.xaml and relative source for the AddClick command so that AddUser_Click can be found on Window.

Update your Window.xaml AddClick binding to

<local:Header AddClick="{Binding Path=DataContext.AddUser_Click, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}" LableContent="Users"/>

and set DataContext of Window.xaml to Window.xaml.cs by adding this to your MainWindow constructor

this.DataContext = this;

Doing the above step will ensure that the AddUser_Click property can be found correctly.

  1. All dependency properties when binded tries to find a property in the DataContext and not method. So, the command should be a property on window.cs of type ICommand and it should be given a method in the constructor.

To implement this most people use http://www.wpftutorial.net/delegatecommand.html. Simply copy this to a new file. In your MainWindow.xaml.cs, add this

AddUser_Click = new DelegateCommand(AddUserMethod);

You can now add a method named AddUserMethod in the same file and it will be called whenever you click the button from the User Control!!

Upvotes: 1

Related Questions