2

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!

1
  • 2
    Take a look at MVVM; this would allow you to store your data within a common ViewModel which could be shared amongst your Views (UserControls). More often then not a UserControl is a carryover from WinForms thinking. Commented Oct 14, 2011 at 14:08

2 Answers 2

3

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:

public int AgreementID
{
    get { return (int) GetValue(AgreementIDProperty ); }
    set { SetValue(AgreementIDProperty, value); }
}
Sign up to request clarification or add additional context in comments.

Comments

2

As already indicated, your property wrapper should be calling GetValue and SetValue on the dependency property (and you can dispense with the backing field). However, it is worth pointing out that the wrapper property will be bypassed when the DependencyProperty is set directly in XAML or from a declarative Binding. So if you actually checked the value of the DependencyProperty itself using GetValue then you would see that the value was actually being set by your Binding. It's the retrieval of the property value via your wrapper in procedural code that's failing since it's not accessing the DependencyProperty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.