0

I have a UserControl called BranchFilter, with the following property:

private int? _branchId; public int? LocalBranchId { get { return _branchId; } set { SetProperty(ref _branchId, value); OnBranchChanged(); } } 

In the same control, I register a dependency property so that I can bind the branch id to a viewmodel:

public static readonly DependencyProperty BranchIdProperty = DependencyProperty.Register(nameof(LocalBranchId), typeof(int?), typeof(BranchFilter), new UIPropertyMetadata(null)); 

And when I try and access this property, without even binding it, in a view, like so:

<controls:BranchFilter Grid.Row="0" BranchId="0"> <i:Interaction.Triggers> <i:EventTrigger EventName="BranchChanged"> <i:InvokeCommandAction Command="{Binding LoadItems}" /> </i:EventTrigger> </i:Interaction.Triggers> </controls:BranchFilter> 

I get the compile errors:

The member "BranchId" is not recognized or is not accessible.

The property 'BranchId' was not found in type 'BranchFilter'.

The property 'BranchId' does not exist in XML namespace 'clr-namespace:ApptBook.Client.Modules.Common.Controls'

I have followed every example, but they're all the same, for adding the dependency property, but everything I have tried has failed. What could be wrong with something so simple?

5
  • 2
    I guess it should be LocalBranchIdProperty Commented Feb 1, 2017 at 13:20
  • 1
    Use propdp code-snippet to generate dependency property properly. Commented Feb 1, 2017 at 13:24
  • @Sinatr I don't have the snippet. Commented Feb 1, 2017 at 13:35
  • @ProfK, I am sure you have. Type propdp and hit Tab key twice. Commented Feb 1, 2017 at 13:42
  • Note that a correct dependency property declaration requires a CLR property wrapper that calls GetValue and SetValue in its get and set accessors. You can not declare a dependency property that wraps a CLR property. See Custom Dependency Properties. Commented Feb 1, 2017 at 13:50

2 Answers 2

2

Should you not be accessing it using LocalBranchId instead of BranchId?

<controls:BranchFilter Grid.Row="0" LocalBranchId="0"> <i:Interaction.Triggers> <i:EventTrigger EventName="BranchChanged"> <i:InvokeCommandAction Command="{Binding LoadItems}" /> </i:EventTrigger> </i:Interaction.Triggers> </controls:BranchFilter> 

I would also rename the DependencyProperty from BranchIdProperty to LocalBranchIdProperty.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, yes. I misunderstood the dependency property registration to create a wrapper around the POCO property, but it actually registers the POCO property as a dependency property, so I should still access the POCO one, and it will have the behaviour of a dependency property.
1

You should correctly declare LocalBranchId as a dependency property:

public static readonly DependencyProperty LocalBranchIdProperty = DependencyProperty.Register( nameof(LocalBranchId), typeof(int?), typeof(BranchFilter)); public int? LocalBranchId { get { return (int?)GetValue(LocalBranchIdProperty); } set { SetValue(LocalBranchIdProperty, value); } } 

If you need to get notified when the property value changes, you can register a PropertyChangedCallback by PropertyMetadata:

public static readonly DependencyProperty LocalBranchIdProperty = DependencyProperty.Register( nameof(LocalBranchId), typeof(int?), typeof(BranchFilter), new PropertyMetadata(LocalBranchIdPropertyChanged)); public int? LocalBranchId { get { return (int?)GetValue(LocalBranchIdProperty); } set { SetValue(LocalBranchIdProperty, value); } } private static void LocalBranchIdPropertyChanged( DependencyObject obj, DependencyPropertyChangedEventArgs e) { var control = (BranchFilter)obj; var id = (int?)e.NewValue; ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.