I have two custom UserControl in my project code: TableControl and DeckControl. In the code of the latter I would be able to access the former when it’s needed. So in my DeckControl I implemented the following property:
private TableControl m_Table;
public TableControl Table
{
get { return m_Table; }
set { m_Table = value; }
}
The problem is that I’m not able to set the property from XAML code:
<Canvas Core:Name="Layout" Loaded="OnLayoutLoaded">
<Namespace:TableControl Core:Name="Table" Canvas.Left="0" Canvas.Top="0" Height="{Binding ElementName=Layout, Path=ActualHeight}" Width="{Binding ElementName=Layout, Path=ActualWidth}"/>
<Namespace:DeckControl Core:Name="Deck" Canvas.Left="50">
</Canvas>
I tried using Reference but compiler says that the method or opera
<Namespace:DeckControl Core:Name="Deck" Canvas.Left="50" Table="{Core:Reference Name=Table}">
I tried this but it isn’t working either:
<Namespace:DeckControl Core:Name="Deck" Canvas.Left="50" Table="{Core:Static Table}">
I also tried using Binding:
<Namespace:DeckControl Core:Name="Deck" Canvas.Left="50" Table="{Binding ElementName=Table}">
Ok so… it’s my first approach to XAML and I’m still working on it… but I really can’t get it!
If you want to bind to a property in youe Model(window/Usercontrol) codebehind you have to set the
DataContextin yourXaml.There are may ways to do this but the simpliest is just naming your window or usercontrol and binding using
ElementName.Example for a Window:
And if you want the
Xamlto update when Table changes your code behind should implementINotifyPropertyChanged, this will inform theXamlthat the property has changed.If your Table property is not a DependancyProperty you will have to chage this so you can bind.
Example:
Also any property that is being binded outside the scope of the UserControl has to be a DependancyProperty.
Example:
This will bind inside the usercontrol when it is a simple property as it is inscope.
This will not bind as its out of scope of the UserControl, MyProperty will have to be a DependancyProperty to bind here
Hope that makes sense 🙂