user3430328
user3430328

Reputation:

Is it possible to bind a XAML control to a corresponding control in the ViewModel?

I am using WPF with the intent of adhering to the MVVM model. I have multiple controls in my XAML view, such as buttons, combo boxes, etc, and I have a corresponding set of controls in my ViewModel. Is there a way to bind a control (such as a button, for example) in XAML to a matching control in the ViewModel, so they have a one-to-one relationship?

I am aware of how it is usually done, with specific XAML control properties being bound to specific properties within the ViewModel. I thought that by binding an entire control to a matching control, it would simplify the binding process. I wouldn't need to bind individual properties, I could just bind the entire control and then programmatically modify any properties I wanted to in the ViewModel.

For example, my XAML button control would look something like this (the DataBinding property is not an actual property):

<Button DataBinding="SignInButton" />

And all the properties for the button would not be set in XAML, but rather in my ViewModel:

public Button SignInButton;
SignInButton.Content = "Sign In";
SignInButton.Width = 100;
SignInButton.FontFamily = "Verdana";

Upvotes: 0

Views: 246

Answers (1)

krishnamoorthy
krishnamoorthy

Reputation: 1

Expose the controls in ViewModel as ContentControl Properties and Bind them in XAML.

Upvotes: 0

Related Questions