0

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

can be with an example, please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.