I have the following problem: I made a UserControl in WPF which has an AgreementID property. I need to call this UserControl in another UserControl, and I need to give an AgreementID with it when I call it. Now I made a DependencyProperty in the first UserControl, but it does not work.
This is the code of my first UserControl:
public partial class UC1001_AgreementDetails_View : UserControl
{
private UC1001_ActiveAgreementContract _contract;
private int _agreementID;
public int AgreementID
{
get { return _agreementID; }
set { _agreementID = value; }
}
public UC1001_AgreementDetails_View()
{
InitializeComponent();
}
public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
This is the XAML where I call previous UserControl:
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<!--<Setter Property="Text" Value="{Binding Months[9].AgreementID}"/>-->
<Setter Property="Tag" Value="{Binding Months[9].AgreementID}"/>
<Setter Property="Background" Value="White" />
<Setter Property="DataGridCell.ToolTip">
<Setter.Value>
<my:UC1001_AgreementDetails_View Background="#FFF" Opacity="0.88" AgreementID="{Binding Months[9].AgreementID}"/>
</Setter.Value>
</Setter>
So I want to pas the AgreementID of that month onto the first UserControl, but when I check it, the passed value is always 0, but it should be 3. I checked the Months[9].AgreementID value and it was 3 indeed. So I get the right value in the binding, but it’s not passing through right.
I hope my problem is a bit clear to you, additional information can be given if wanted!
You are defining the Property facade for the Dependency Property wrong. In your property definition, you need to work on the Dependency Property and set and return its value: