Is it possible to use trigger setter to change Vie

2019-06-04 07:25发布

问题:

I am opening Popup using IsOpen bound to some hard to reach attached property. All I want is to somehow pass IsOpen value to ViewModel.

Trying to use setter for this:

<Popup StaysOpen="False"
       IsOpen="{Binding Path=(local:ListViewBehavior.IsColumnHeaderClicked), RelativeSource={RelativeSource FindAncestor, AncestorType=GridViewColumnHeader}}">
    <Popup.Style>
        <Style TargetType="Popup">
            <Style.Triggers>
                <Trigger Property="IsOpen" Value="True">
                    <!-- this is not possible -->
                    <Setter Property="{Binding IsPopupOpened ...}" Value="True" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Popup.Style>
</Popup>

Gives:

A 'Binding' cannot be set on the 'Property' property of type 'Setter'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

Is there a way to pass IsOpen value (which is already bound to something else in the View) to ViewModel?

回答1:

You could created an attached Property, which is set by eventhandlers to the Opened and Closed Events of the Popup. That can be bind to the IsPopupOpen Property of your ViewModel by OneWayToSource Binding. I'm also not quite sure if there is an easier Solution, but this is a kind of "Workaround" what I would do, in case that no-one provides a better solution here.