【问题标题】:Using purely XAML to set a Dependency Property of a Control on button click使用纯 XAML 在按钮单击时设置控件的依赖属性
【发布时间】:2023-03-06 19:18:01
【问题描述】:

所以我仍在尝试找出使用 MVVM 的 WPF 的局限性。现在我想知道是否可以绑定按钮单击以触发对另一个控件依赖项属性的更改,而无需触摸后面的代码。这是我的 Xaml 示例。

<xctk:WindowContainer>
    <xctk:ChildWindow IsModal="True" WindowStartupLocation="Center" WindowState="Open">
         <view:EditView/>
    </xctk:ChildWindow>
</xctk:WindowContainer>

现在在这个编辑视图中,我有一些字段和一个提交和取消按钮,它与其父级共享相同的视图模型。

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
    <Button Content="Submit"/>
    <Button Content="Cancel"/>
</StackPanel>

现在通常我会在 ViewModel 中发出一个命令来执行我想要的行为,但是我想如果我可以在点击取消按钮时将 ChildWindow 的 WindowState 属性设置为 Closed 会不会很好?我不知道这是否可以轻松完成,但我想我会问,因为这似乎应该是可能的,但我对 WPF 的了解还不存在。我很感激任何意见。

【问题讨论】:

  • 如果您有交互/交互命名空间Microsoft.Expression.Interactivity.Core,您可以使用ChangePropertyAction 并使用EventTrigger 轻松完成,或者如果您更愿意使用Close 方法,可以只使用CallMethodAction
  • WindowState 属性没有名为 closed 的内容。

标签: .net wpf xaml


【解决方案1】:

您可以尝试使用EventTriggerStoryboard

<xctk:WindowContainer>
    <xctk:ChildWindow x:Name="childWindow">
        <view:EditView/>
    </xctk:ChildWindow>
</xctk:WindowContainer>

...

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
    <Button Content="Submit"/>
    <Button Content="Cancel">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="childWindow" Storyboard.TargetProperty="WindowState">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static xctk:WindowState.Closed}" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
             </EventTrigger>
        </Button.Triggers>
    </Button>
</StackPanel>

http://dotnet.dzone.com/articles/event-based-property-changes

【讨论】:

  • 哇,感谢您的快速回复,这正是我想要的,虽然我觉得很有趣,您需要使用情节提要。
猜你喜欢
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 2012-06-01
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多