WPF binding to UserControl Custom DependencyProperty

I have a custom UserControl called SongDescription:

<UserControl x:Class="DPTestAp.SongDescription" ...> <Grid x:Name="LayoutRoot"> <DockPanel Height="50"> <TextBlock x:Name="title" Text="{Binding name}" Width="100" Height="30"/> <TextBox x:Name="lyrics"/> </DockPanel> </Grid> </UserControl> 

I added DependencyProperty to it:

 public partial class SongDescription : UserControl { public static readonly DependencyProperty SongProperty = DependencyProperty.Register("Song", typeof(Song), typeof(SongDescription)); public Song Song { get { return (Song)GetValue(SongProperty); } set { SetValue(SongProperty, value); updateLyrics() } } private void updateLyrics() { lyrics.Text = Song.lyrics; } public SongDescription() { InitializeComponent(); } } 

Question: how to connect something with this SongProperty? I use SongDescription in my main window as follows:

 <local:SongDescription x:Name="songDescription" Song="{Binding DataContext}"/> 

I can not make text TextBox lyrics. In the main window, I tried to set the DataContext to songDescription, for example:

 songDescription.DataContext = new Song() { name="Home", lyrics="Hold on, to me as we go" }; 

or in order to make the window look:

 DataContext = new Song() { name="Home", lyrics="Hold on, to me as we go" }; 

I even tried to make Song a resource and associate it with SongProperty as follows:

 <Window.Resources> <local:Song x:Key="res" name="Home" lyrics="Hold on, to me as we go"/> </Window.Resources> <Grid> <local:SongDescription x:Name="songDescription" Song="{StaticResource res}"/> </Grid> 

Nothing helped. The TextBlock header binds the song name in order. But I can not call the updateLyrics () method. (In real life, this method is more complicated, so I can not use Binding, as with a name).

Thanks!

+6
source share
2 answers

Yup, so getcha with dependent properties. You never added verification code inside accessor methods (get / set), because dependency properties are stored in WPF in a table that it manages. That's why you have to register dependency properties, it essentially creates records in this table to store the values โ€‹โ€‹associated with each property, and when you use "GetValue" / "SetValue", you update the records in this table (which, by the way, refers to how WPF can manage data bindings in general).

Thus, this means that WPF can (and will) completely bypass your accessory properties, since it has direct access to real data. Why should he use your accessors if he can just jump to the data directly. Instead, you need to implement a PropertyChanged callback function or some WPF-approved validation method, but never do this in your accessories.

Cm:

http://msdn.microsoft.com/en-us/library/ms752914.aspx

+7
source

In addition to sircodesalot's answer, you are not tied to the text box of your lyrics. In addition, since the song that you are attached to the class, you will need to specify the paths completely for the properties that you want to show in fields such as "Path = Song.Lyrics".

Another thing to consider is dependency properties; your mode will be enabled by default, so editing a text field will be relevant if you do not change it.

Thirdly, if you use MVVM, you only need your main window context, which must be set to the view model and have the corresponding Song property for binding.

+2
source

All Articles