同时访问数据上下文和代码隐藏中的属性
我正在使用 MVVM/WPF 并尝试做一些看似简单的事情,但无法找到一个干净的解决方案。
我想要执行以下操作:
当模型中的属性发生更改(在这种情况下 WPF 文本框文本将发生更改)时,使用一种方法在 UI 上执行与属性绑定相关的其他操作。
目前我正在工具提示上使用多重绑定(以获取文本框数据上下文+绑定路径),但这有点像黑客。
<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 事件。
如果有人能指出我正确的方向,我将不胜感激。
I am using MVVM/WPF and trying to do something seemingly simple, but cant find a clean solution.
I want to do the following:
When a property changes in the model (WPF Textbox text would be changed in this case), use a method to perform other operations on the UI relating to the property bound.
Currently i am using a multibinding on the tooltip (to get the textbox datacontext + binding path), but this is a bit of a 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>
I would like to make something re-usable and maybe for different controls, hence i am not just using the textchanged event.
If anyone could point me in the right direction, it would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,就您的多重绑定而言,您想在那里完成什么?我不知道你的转换器应该做什么,但是不能用 IValueConverter 实现类来完成吗?我假设不是,看起来您正在将文本框传递给转换器。
至于在模型属性更新时让方法执行多项操作,您可以让视图模型订阅模型类上的事件。只需声明对象 WithEvents (VB.NET) 并为 On[PropertyName]Changed 添加事件处理程序。
在实现 MVVM 时,我倾向于将代码隐藏视为二等公民。如果可以的话,我会尽力将所有逻辑推到 ViewModel 或 View 中。我几乎完全停止使用转换器,因为大部分逻辑都可以在 ViewModel 中复制,如果我想重复使用它,我通常只有一个小帮助器类,它可以获取传递给它的任何内容,执行某些操作,然后将其传回。我从来没有真正与 IValueConverter 建立过如此好的关系......
除此之外,还不清楚你到底想做什么。我们能得到更多澄清吗?
OK, so far as your Multibinding there, what are you trying to accomplish there? I don't know what your converter is supposed to do, but can it not be done with an IValueConverter implementing class? I am assuming not, it looks like you are passing the textbox to the converter.
As far as having a method do several things when your model properties get updated, you can have the viewmodel subscribe to events on your model class. Just declare the object WithEvents (VB.NET) and add event handlers for On[PropertyName]Changed.
When implementing MVVM, I tend to treat the codebehind as a second class citizen. I do my best to push all logic off to the ViewModel or View if I can. I have almost completely stopped using Converters as much of that logic can be duplicated in ViewModels, and if it is something that I want to re-use, I usually just have a little helper class that gets whatever passed to it, does something, and passes it back out. I have never really had that great a relationship with IValueConverter...
Other than that, it is unclear exactly what you are trying to do. Could we get some more clarification?
看起来您正在尝试让工具提示包含文本框的内容,如果是这样,为什么不这样做呢?
如果这不是您想要的,但希望工具提示根据文本框的值进行更改,请在您的视图模型中执行此操作,例如
,然后在您的标记中执行此操作:
It looks like you're trying to have the tooltip have the content of the textbox, if so why not just do this?
If that's not what you want, but want the tooltip to change based on the value of the textbox then do that in your viewmodel e.g.
and then in your markup: