【问题标题】:Is it possible to use trigger setter to change ViewModel property?是否可以使用触发器设置器来更改 ViewModel 属性?
【发布时间】:2016-03-18 09:15:36
【问题描述】:

我正在使用IsOpen 打开弹出窗口,该IsOpen 绑定到一些难以到达 附加属性。我想要的只是以某种方式将 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>

给予:

不能在“Setter”类型的“Property”属性上设置“Binding”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。

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

【问题讨论】:

  • &lt;Style TargetType="Popup"&gt;&lt;Setter Property="IsOpen" Value="{Binding VMProperty}"&gt;&lt;/Style&gt;怎么样
  • 他已经绑定到 IsOpen 属性...
  • @FrerkMorrin 哇,抱​​歉。我需要更多的睡眠。

标签: c# wpf mvvm binding


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 2018-07-07
    • 2011-10-23
    • 2013-04-09
    • 2010-11-04
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    相关资源
    最近更新 更多