使用 DataBinding 值更新控件的宽度 (WPF)
我创建了一个用户控件和一个关联的视图模型。 视图模型的属性“DisplayName”和“TimeIntervalLength”显示在用户控件视图DataBinding中。 根据视图模型的这些属性,我想更新控件的宽度。 宽度应为“TimeIntervalLength”,但至少应为“DisplayName”。 我尝试覆盖“OnPropertyChanged”,但这不起作用。 此外,我找不到合适的事件来覆盖。
提前致谢。
I created a user control and an associated view model. The properties "DisplayName" and "TimeIntervalLength" of the view model are displayed in the user control view DataBinding. Depending on those properties of the view model I want to update the width of my control.
The width should be "TimeIntervalLength" but at least "DisplayName". I tried to override "OnPropertyChanged" but this does not work. Further I could not find an appropriated event to override.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试不在用户控件上指定宽度/高度。 它应该适合其中托管的控件。
另一种选择是使用 IValueConverter 来做一些更繁重的工作:
在 MaxValueConverter 中进行繁重的工作:
You may try just not specifying a Width/Height on your UserControl. It should fit to the controls hosted within.
Another option is to use an IValueConverter to do some heavier lifting:
Heavy lifting in your MaxValueConverter:
不完全确定我是否理解正确,但听起来最简单的方法是将计算属性添加到 ViewModel 中,然后从视图绑定到该属性:(
显然,您可以将“CalculateDisplayWidth()”替换为您需要的任何内容)
Not entirely sure if I understand correctly, but it sounds like the easiest thing to do would be to add a calculated property to your ViewModel and then bind to that from the View:
(Obviously you can replace "CalculateDisplayWidth()" with whatever you need)
控制 MinWidth,而不是控制宽度。 并在 UserControl 的构造函数中清除 Width 属性:
Instead of controlling Width, control MinWidth. And clear the Width property in the constructor of UserControl: