开发者

wpf: bind to a style property on main window from UserControl

开发者 https://www.devze.com 2023-01-03 17:57 出处:网络
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

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 开发者_C百科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 ?


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 ...
0

精彩评论

暂无评论...
验证码 换一张
取 消