【发布时间】:2011-07-19 21:40:38
【问题描述】:
我正在使用 MVVM/WPF 并尝试做一些看似简单的事情,但找不到干净的解决方案。
我想做以下事情:
当模型中的某个属性发生变化时(在这种情况下会更改 WPF 文本框文本),使用方法在 UI 上执行与属性绑定相关的其他操作。
目前我在工具提示上使用多重绑定(获取文本框数据上下文 + 绑定路径),但这有点 hack。
<TextBox x:Name="textBox" Text="{Binding Model.MyProperty}">
<TextBox.ToolTip>
<MultiBinding Converter="{StaticResource brNewMultiConverter}">
<!-- This to trigger the converter in all required cases.
Without it, i cant get the event to fire when filling
the model initially
-->
<Binding ElementName="textBox" Path="Text" />
<!-- This has the properties i need, but wont fire without
the binding above -->
<Binding ElementName="textBox" />
</MultiBinding>
</TextBox.ToolTip>
</TextBox>
我想做一些可重复使用的东西,也许可以用于不同的控件,因此我不只是使用 textchanged 事件。
如果有人能指出我正确的方向,将不胜感激。
【问题讨论】:
-
"使用方法在 UI 上执行与属性绑定相关的其他操作。"如果这是具体的,就可以就如何实现这一点提供建议。
标签: wpf mvvm multibinding valueconverter