问题描述
我是WPF和MVVM的新手,只要ViewModel将属性commandToExec
设置为true,我就需要在View中执行命令propertyToSet
。现在,我有一个设置了propertyToSet
时可见的Stackpanel,以及绑定到命令commandToExec
的那个Stackpanel中的一个按钮,还有一个在该按钮上的Datatrigger,如果{{1 }}已设置。大致如下所示:
propertyToSet
这是行不通的,而且似乎不直观且笨拙,因此我敢肯定有更好的方法可以做到这一点。我对XAML不太了解,所以我只是对这些代码进行了总结,但是我想做这样的事情:
<StackPanel Visibility="{Binding propertyToSet,Converter={StaticResource BooleanToVisibilityConverter}}">
<Button
Command="{Binding commandToExec}">
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding commandToExec,Converter={StaticResource BooleanToVisibilityConverter}}">
<Setter Property="Button.IsPressed" Value="True" /> //Trying to set button isPressed to true to trigger command
</DataTrigger>
</Style.Triggers>
</Style>
</Button>
</StackPanel>
因此,只要StackPanel通过 <StackPanel Visibility="{Binding propertyToSet,Converter={StaticResource BooleanToVisibilityConverter}}">
<SomethingThatAutomaticallyDoesCommand Command="{Binding commandToExec}"/>
<StackPanel/>
可见,任意对象SomethingThatAutomaticallyDoesCommand就会执行其工作并运行命令。
我见过的另一个骇人听闻的主意是,只在StackPanel上设置一个标签即可根据propertyToSet对其进行更新,并以某种方式将我的命令绑定到TargetUpdated,如[here] [1]所示。
无论如何,我希望这是有道理的。非常感谢! [1]:Can i call a function via DataTrigger (XAML)?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)