4

I got some problem with custom dependency property binding. We have: Custom user control with one dependency property and binding to self:

<UserControl x:Class="WpfApplication1.SomeUserControl"
         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="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
    <Label>
        <Label.Template>
            <ControlTemplate>
                <Label Content="{Binding MyTest}"/>
            </ControlTemplate>
        </Label.Template>
    </Label>
</Grid>

... and code of control:

public partial class SomeUserControl : UserControl
{
    public SomeUserControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty MyTestProperty = DependencyProperty.Register("MyTest", typeof(int), typeof(SomeUserControl));

    public int MyTest
    {
        get { return (int)GetValue(MyTestProperty); }
        set { SetValue(MyTestProperty, value); }
    }
}

I trying to use this control with binding to some simple property of simple model class:

<UserControl x:Class="WpfApplication1.AnotherUserControl"
         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:wpfApplication1="clr-namespace:WpfApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <wpfApplication1:SomeUserControl MyTest="{Binding Path=Model.MyNum}" Grid.Column="0"/>
    <Label Content="{Binding Path=Model.MyNum}" Grid.Column="1"/>
</Grid>

... with code

public partial class AnotherUserControl : UserControl
{
    public MyModel Model { get; set; }
    public AnotherUserControl()
    {
        Model = new MyModel();
        Model.MyNum = 1231;
        InitializeComponent();
    }
}

... and model:

public class MyModel:INotifyPropertyChanged
{
    private int _myNum;

    public int MyNum
    {
        get { return _myNum; }
        set { _myNum = value; OnPropertyChanged("MyNum");}
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

But binding is not working. Stack have no errors in compilation. Now, binding working with statndart wpf label control (with same model), and don't working with my custom control and custom property. Please help me to understand reasons of this problem and solve it; Thanks)

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
kirilljk
  • 54
  • 1
  • 4

2 Answers2

6

You should use ElementName Binding in your SomeUserControl.

<UserControl x:Class="WpfApplication1.SomeUserControl"
     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="300" d:DesignWidth="300" 
     x:Name="uc">

<Grid>
    <Label>
        <Label.Template>
            <ControlTemplate>
                <Label Content="{Binding MyTest, ElementName=uc}"/>
            </ControlTemplate>
        </Label.Template>
    </Label>
</Grid>

here some more information why you should not set the datacontext of a usercontrol to self/this. Data Binding in WPF User Controls

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74
2

You will need to update your binding as below i.e you will have to bind to the ancestor usercontrol property using RelativeSource as you are setting the DataContext of child usercontrol explicitly, default binding will search for path inside the child userControl's DataContext.

        <wpfApplication1:SomeUserControl MyTest="{Binding Path=DataContext.Model.MyNum, RelativeSource={RelativeSource FindAncestor={x:Type UserControl}}}" Grid.Column="0"/>
Nitin
  • 18,344
  • 2
  • 36
  • 53
  • No. That's no working. Binding in control template is correct. Because if I assign value to "MyTest" without binding then Control working correct. Not working external binding from another place. But I think on ControlTemplate level all right. – kirilljk Oct 09 '13 at 04:35
  • my bad.. didnt see that you are explicitly setting the datacontext of child usercontrol to itself.. updated answer should work – Nitin Oct 09 '13 at 04:42
  • It's working. Thanks) But Is it possible to avoid RelativeSource declaration? – kirilljk Oct 09 '13 at 04:53
  • in this scenerio you will have to tell the binding where to look for source, as default will always look inside its DataContext – Nitin Oct 09 '13 at 05:12