User9995555
User9995555

Reputation: 1556

WPF UserControls Properties

I have a question regarding WPF UserControls and MVVM Pattern, I need to expose a Property form my ViewModel so that it is visible in the Properties box (when the UserControl is placed on the MainWindow) so that the user of my WPF Control can set the value of the new property at design time.

This is easy when I put my custom Property get/set in the UserControl.xaml.cs file but this seems to violate MVVM rules.

Normally I can find the answer to most of the problems I come across but I am stumped now.

Upvotes: 0

Views: 3622

Answers (1)

Kieren Johnstone
Kieren Johnstone

Reputation: 41983

You want to create a dependency property. Here's a good guide:

http://msdn.microsoft.com/en-us/library/ms752914.aspx

Be sure to read through fully and understand that the get/set won't be called for example with bindings - the internal property system will handle that. Hope that helps :)

...

Just noticed you're talking about adding a property to the ViewModel - but then talk about a .xaml.cs file. Those are different things, the .xaml.cs is part of the View (XAML+code-behind) - it should bind to a ViewModel. Are you sure you have everything correct?

Upvotes: 6

Related Questions