c# - Binding UserControl property to Window property -


I am struggling with some compulsive issues. I have a UserControl with a TextBlock text text is bound to a property when using UserControl in the window and setting up the property with some text, everything works as expected, but when I open the window I bind that property in another property, then nothing happens. This is an example that shows this problem:

UserControl xaml:

  & lt; UserControl x: class = "TestBinding.UserControl1" xmlns = "http: // schemos Microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml "Xmlns: mc =" http://schemas.openxmlformats.org/markup-compatible / 2006 "xmlns: d =" http://schemas.microsoft.com/expression/blend/2008 "mc: Ignorable =" d " D: design heite = "300" D: DesignWidth = "300" & gt; & Lt; Textbox text = "{some binding some text}" /> UserControl Code: Public Partial Class UserControl1: UserControl {Public Dependency Dependency property for certain static readingProperty = DependencyProperty.Register ("SomeText", typef (string), typef (user control 1), new Property Matadata ("Default Text Control")); Public string Get some text {{return (string) this.GetValue (SomeTextProperty); } Set {this.SetValue (SomeTextProperty, value); }} Public UserControl1 () {this.DataContext = this; InitializeComponent (); }}  

Window xaml:

  & lt; Window x: Class = "Testboding .MyWindows" xmlns = "http://schemas.microsoft.com/enfx/ 2006/xaml/Presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "xmlns: local =" clr-namespace: testbanding "title =" main window "height =" 350 "width =" 525 "& gt; & Lt; Grid & gt; & Lt; Grid.ColumnDefinitions & gt; & Lt; Column width = "1 *" /> & Lt; Column width = "1 *" /> & Lt; /Grid.ColumnDefinitions> & Lt; Grid.RowDefinitions & gt; & Lt; RowDefinition Height = "1 *" /> & Lt; RowDefinition Height = "1 *" /> & Lt; /Grid.RowDefinitions> & Lt; Local: UserControl1 Grid.Row = "0" grid. Column = "0" /> & Lt; Local: UserControl1 Grid.Row = "1" grid. Column = "0" some text = "another text" /> & Lt; TextBlock Grid.Row = "0" grid. Column = "1" text = "{some binding Bible}" /> & Lt; Local: UserControl1 Grid.Row = "1" grid. Column = "1" some text = "{some bindingEND_BOLD} />   
  public partial class main window: windows {public static reading For the dependency property some textbook = dependency property. Registrar ("some text", typef (string), typef (main wando), new property matadata ("window default text")); public string some text {{return (strings) Get this.GetValue (SomeTextProperty);} set { This.SetValue (SomeTextProperty, value);}} Public main window () {this.DataContext = this; InitializeComponent ();}}  

What do I expect to see in the bottom right corner

Thank you for your help < Text ">

When you set UserControl1.SomeText in the text, then it works because then the binding reference does not matter, but when you use binding And do

  public UserControl1 () {this.DataContext = this; ....}  

You can overwrite DataContext , which is usually there for the UserControl1 binding inheritance and change reference Will be through. Remove the above line and replace the XAMML as follows:

  & lt; UserControl ... x: name = "myUserControl" & gt; & Lt; Text block text = "{binding element name = myUserControl, path = some text}" />  

Comments