Theun Arbeider
Theun Arbeider

Reputation: 5409

Custom usercontrol property binding failure silverlight

I have a custom usercontrol with DataContext="{Binding RelativeSource={RelativeSource self}}"

On the code behind i've made a dependency property like:

    public static DependencyProperty ElementNameProperty = DependencyProperty.Register("ElementName",
        typeof(string),
        typeof(ElementControl),
        new PropertyMetadata(new PropertyChangedCallback((s, e) => { new Base().OnPropertyChanged("ElementName"); })));

    public string ElementName
    {
        get
        {
            return (string)base.GetValue(ElementNameProperty);
        }
        set
        {
            base.SetValue(ElementNameProperty, value);

        }
    }

Now when I try to use this usercontrol in my mainpage.xaml and use the following binding: <test.TestControl ElementName="{Binding name}" />, it keeps searching for 'name' property in my custom usercontrol instead of where it should come from?

What am I doing wrong ?

Upvotes: 0

Views: 743

Answers (2)

Theun Arbeider
Theun Arbeider

Reputation: 5409

I eventually solved it this way. Not the way I wanted, but it's a (in my eyes) pretty neat solution.

CustomUserControl.xaml

<UserControl x:Class="TestApp.Controls.CustomUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         Width="75"
         Height="75">
  <Canvas x:Name="LayoutRoot"
      Background="Black">
    <StackPanel Orientation="Vertical">
      <Image x:Name="UCImage"
         Width="50"
         Height="50"
         HorizontalAlignment="Center" />
      <TextBlock x:Name="UCText"
             HorizontalAlignment="Center" />
    </StackPanel>
  </Canvas>
</UserControl>

CustomUserControl.xaml.cs

public partial class ElementControl : UserControl
{
    #region DependencyProperty ElementNameProperty
    public static DependencyProperty ElementNameProperty = DependencyProperty.Register("ElementName",
        typeof(string),
        typeof(ElementControl),
        new PropertyMetadata(new PropertyChangedCallback((s, e) => 
    { 
    //See Here
    ((ElementControl)s).UCText.Text = e.NewValue as string; 
    })));

    public string ElementName
    {
        get
        {
            return (string)base.GetValue(ElementNameProperty);
        }
        set
        {
            base.SetValue(ElementNameProperty, value);
        }
    }
    #endregion
}

Upvotes: 0

dmusial
dmusial

Reputation: 1564

It searches there because you have the DataContext set on the topmost level for your user control. What you would need to do is get rid of the relative binding to self in the user control and specify ElementName in bindings (inside user control). Btw you probably don't need OnPropertyChanged in the PropertyChangedCallback cause DependencyProperties in their nature notify about value changes.

Upvotes: 1

Related Questions