tags:

views:

44

answers:

1

I have a UserControl which has a style, that i would like to be influenced by a settings checkbox on the main window hosting my user control

so myControl.xaml has a Style which i would like to have a trigger, that should observe a CheckBox inside MainWindow.xaml

i know one way to do this, would be to create a local property in myControl.cs which would look at the property in MainWindow.cs which would in turn return state of that cheeckbox.. but maybe there is a way to do this w/out writing any c# code ?

A: 

You should bind the checkbox in your main window to a property of a "view model" object. In the trigger on the UserControl, bind to the same property of the same "view model" object.

If your "view model" object is set as the DataContext of the main window, it will also be available in the UserControl.

In main window:

<CheckBox Content="Click here if you are happy"
          IsChecked="{Binding UserIsHappy}" />

In the UserControl:

<Style>
  ...
  <DataTrigger Binding="{Binding UserIsHappy}" Value="True">
    ... custom changes for happy people here ...
Ray Burns
how do i set the view model object ?
Sonic Soul
The most common way is to set an application's main view model in the constructor of the main window: `DataContext = new MyViewModel();`
Ray Burns