将 DataTrigger 应用于其绑定来自另一个控件的按钮?
我正在尝试将 DataTrigger
应用于 Button
,它取决于 TreeView
当前所选项目的属性。 我的想法是,我想根据所选项目的属性更改 Button
的文本。
我所拥有的看起来像这样:
<Button x:Name="m_AddObject" Margin="192.708,0.909,6,6.363" Click="AddObject_Click" >
<DataTrigger Binding="{Binding ElementName=ObjectTreeView, Path=SelectedItem.Removable}" Value="true">
<Setter TargetName="m_AddObject" Property="Content" Value="Remove" />
</DataTrigger>
</Button>
但我无法编译它。 Setter 抱怨“Content”无效,因为它没有限定类型名称,但如果我将其更改为“Button.Content”,它就会抱怨“对象引用未设置为对象的实例”。
我也尝试过:
<Setter TargetName="m_AddObject.Content" Value="Remove" />
虽然可以编译,但也不起作用。
我很困惑。
有任何想法吗?
谢谢!
I'm trying to apply a DataTrigger
to a Button
and it depends on a property from the currently selected item of a TreeView
. The idea is that I want to change the text of a Button
depending on a property of the selected item.
What I have looks like this:
<Button x:Name="m_AddObject" Margin="192.708,0.909,6,6.363" Click="AddObject_Click" >
<DataTrigger Binding="{Binding ElementName=ObjectTreeView, Path=SelectedItem.Removable}" Value="true">
<Setter TargetName="m_AddObject" Property="Content" Value="Remove" />
</DataTrigger>
</Button>
But I can't get it to compile. The Setter complains about "Content" not being valid because it doesn't have a qualifying type name, but if I change it to "Button.Content" it then complains of "Object reference not set to an instance of an object".
I also tried:
<Setter TargetName="m_AddObject.Content" Value="Remove" />
While that compiles, it didn't work either.
I'm stumped.
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataTriggers 应在按钮的样式中定义。 上面您尝试执行的操作本质上是使用 DataTriggers 作为按钮的“标签”(“内容”,如 WPF 所说)(而不是“确定”)。
这是临时的,所以它可能不完全正确,但它更接近你想要的:
DataTriggers should be defined in a Style for the button. What you're trying to do above is essentially use a DataTriggers as the "label" (the "Content", as WPF puts it) for the button (instead of, say, "OK").
This is ad-hoc, so it might not be totally correct, but it's closer to what you want: