c# - 是否可以使用触发器 setter 来更改 ViewModel 属性?

标签 c# wpf mvvm binding

我正在使用 IsOpen 绑定(bind)到一些难以访问 附加属性来打开 Popup。我只想以某种方式将 IsOpen 值传递给 ViewModel。

为此尝试使用 setter:

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

给予:

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

有没有办法将 IsOpen 值(已经绑定(bind)到 View 中的其他内容)传递给 ViewModel?

最佳答案

您可以创建一个附加属性,该属性由事件处理程序设置为弹出窗口的打开和关闭事件。这可以通过 OneWayToSource 绑定(bind)绑定(bind)到 ViewModel 的 IsPopupOpen 属性。 我也不太确定是否有更简单的解决方案,但这是我会做的一种“解决方法”,以防没有人在这里提供更好的解决方案。

关于c# - 是否可以使用触发器 setter 来更改 ViewModel 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36080405/

相关文章:

wpf - MVVM Light - 继电器命令参数值间歇性出现

c# - 如何处理检索 i2c 缓冲区和设置模型/实体属性?

wpf - 在哪里放置 MVVM CustomControl ViewModels/类 WPF

SwiftUI 观察到的对象在发布值更改时不更新

c# - ASP.NET Core HttpClient无法在容器中进行HTTPS调用

c# - 如何对枚举值进行分组?

c# - where 和 join 有什么区别?

c# - xpath 和 c#

c# - 如何绑定(bind)到一个数据源但从另一个数据源更新触发器?

c# - 用户控制 + ViewModelBase