Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to custom dependency property - again

Tags:

The task: implement the simplest Dependency Property ever, which can be used in xaml like that:

<uc:MyUserControl1 MyTextProperty="{Binding Text}"/> 

I think that this answer is quite close. For better readability i copy all my code here (mostly from that answer above).

<UserControl x:Class="Test.UserControls.MyUserControl1"              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"               DataContext="{Binding RelativeSource={RelativeSource Self}}">     <Grid>         <!-- Text is being bound to outward representative property;              Note the DataContext of the UserControl -->         <TextBox Text="{Binding MyTextProperty}"/>     </Grid> </UserControl> 

and

public partial class MyUserControl1 : UserControl {     // The dependency property which will be accessible on the UserControl     public static readonly DependencyProperty MyTextPropertyProperty =         DependencyProperty.Register("MyTextProperty", typeof(string), typeof(MyUserControl1), new UIPropertyMetadata(String.Empty));     public string MyTextProperty     {         get { return (string)GetValue(MyTextPropertyProperty); }         set { SetValue(MyTextPropertyProperty, value); }     }      public MyUserControl1()     {         InitializeComponent();     } } 

And this is my MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:uc="clr-namespace:WpfApplication1"         Title="MainWindow" Height="350" Width="525">     <StackPanel Orientation="Vertical">         <uc:MyUserControl1 MyTextProperty="my text goes here"/>         <Button Click="ButtonBase_OnClick" Content="click"/>     </StackPanel> </Window> 

So far, everything works. However, i find this quite not usefull. What i'd need is

<uc:MyUserControl1 MyTextProperty="{Binding Text}"/> 

and being able to change this by setting a DataContext (as you usually do in MVVM)

So i replace the line as above and add my code behind as follows:

public partial class MainWindow : Window, INotifyPropertyChanged {     public MainWindow()     {         InitializeComponent();         Text = "Initial Text";         DataContext = this;     }     private string _Text;     public string Text     {         get { return _Text; }         set         {             if (value != _Text)             {                 _Text = value;                 NotifyPropertyChanged("Text");             }         }     }      private void ButtonBase_OnClick(object sender, RoutedEventArgs e)     {         Text = "clicked";     }      public event PropertyChangedEventHandler PropertyChanged;     private void NotifyPropertyChanged(String info)     {         if (PropertyChanged != null)         {             PropertyChanged(this, new PropertyChangedEventArgs(info));         }     } } 

Neither the "initial Text" nor the "clicked" is displayed... ever. So my question is how to implement a dept. property correctly to be used with

<uc:MyUserControl1 MyTextProperty="{Binding Text}"/> 
like image 592
Martin Booka Weser Avatar asked Dec 19 '12 16:12

Martin Booka Weser


People also ask

What is the difference between property and dependency property?

The primary difference between a dependency droperty and a standard clr property is that a dependency property can be the target of a binding. This allows you to tie the value of the property to a value provided by some other object.

What is difference between dependency property and attached property?

Attached properties allows container to create a property which can be used by any child UI elements whereas dependency property is associated with that particular elements and can help in notification of changes and reacting to that changes.

What is a dependency property?

A dependency property is a specific type of property where the value is followed by a keen property system which is also a part of the Windows Runtime App. A class which defines a dependency property must be inherited from the DependencyObject class.

What is CLR property?

CLR property is just a wrapper around private variables. It uses Get / Set methods to retrieve and store value of a variable into it. A CLR property gives you only one block in which you can write code to invoke whenever a property is get or set.

What is the default data binding mode for a dependency property?

Although the default data binding mode for dependency properties is OneWay, you can change the binding mode of a specific binding to TwoWay. For more information, see Binding direction. As a dependency property author, you can even choose to make two-way binding the default mode.

What is a custom dependency property in WPF?

Dependency properties are properties that are registered with the WPF property system through Register or RegisterReadOnly calls. The Register method returns a DependencyProperty instance that holds the registered name and characteristics of a dependency property.

What is the dependencyproperty identifier?

For example, the corresponding DependencyProperty identifier for the Control.Background property is Control.BackgroundProperty. The identifier stores the information about the dependency property as it was registered, and can then be used for other operations involving the dependency property, such as calling SetValue.

How do I make a property a dependency property?

In order for your property to be a dependency property, you must register it with the property system. To register your property, call the Register method from inside the body of your class, but outside of any member definitions.


1 Answers

The Text property is located on the DataContext of the MainWindow not of the UserControl.

So change this line <uc:MyUserControl1 MyTextProperty="{Binding Text}"/> into this:

<uc:MyUserControl1 MyTextProperty="{Binding Text, ElementName=MyMainWindow}"/> 

Which will tell the Binding that you're talking about the Text element located in you MainWindow. Of course, since in this example I used ElementName, you're going to want to name your window MyMainWindow...

So add this to your MainWindow:

<Window  Name="MyMainWindow" ..... /> 

If you rather not name your window, you can use the RelativeSource FindAncestor binding like this:

<wpfApplication6:MyUserControl1 MyTextProperty="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/> 

In both ways, you are asking to find the property named 'Text' in the DataContext of the window.

like image 73
Blachshma Avatar answered Oct 23 '22 06:10

Blachshma