Winui 3中的样式二传手绑定
winui 3是否支持样式设置器中的绑定?我为导航视图定义了一种样式,第三行是:这是:
<Setter Property="CompactPaneLength" Value="{Binding CurrentCompactPaneLength}" />
这会产生指定的铸件无效。
在运行时例外。包含NavigationView的页面的DataContext是页面的ViewModel。 navigationview.compactpanelength和currentCompactpaneLength是双重和公共和CurrentCompactpaneLength是一个可观察的Obibject(来自communityToolkit.mvvm.componentmodel)。
WINUI 3(SDK 1.1.2)的源代码包括各种设置器,例如
<Setter Target="PaneContentGrid.Width" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CompactPaneLength}" />
在代码工作中进行绑定,如果是必要的,则进行了绑定。但是XAML也不工作吗?
Does WinUI 3 support binding in a Style Setter? I've defined a Style for a NavigationView and the third line is:
<Setter Property="CompactPaneLength" Value="{Binding CurrentCompactPaneLength}" />
This produces a Specified cast is not valid.
exception at run time. The DataContext for the page containing the NavigationView is the ViewModel for the page. Both NavigationView.CompactPaneLength and CurrentCompactPaneLength are double and public and CurrentCompactPaneLength is an ObservableObject (from CommunityToolkit.Mvvm.ComponentModel).
The source code for the WinUI 3 (SDK 1.1.2) includes various Setters, such as
<Setter Target="PaneContentGrid.Width" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CompactPaneLength}" />
Doing the bindings in code works, if that's what's necessary. But shouldn't the XAML work, too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,在Winui 3中尚不支持二传手中的一般绑定,尽管这是一个备受要求的功能。解决方法是创建一个带有依赖关系的助手类,每当属性被更改/设置时,它都会调用变更处理程序。然后,更改处理程序可以在代码中创建所需的绑定。对 clemens 建议UWP以前的建议。这是一个示例助手类:
请注意,所有依赖关系均为字符串类型,目标类型可以是您正在使用的控件的任何祖先类型。例如,
heightbindingpathproperty
可以与任何Frameworkelement一起使用。像这样的二阶,使用样式的助手,这样:
我希望这会有所帮助。
Apparently, general bindings in Setters are not supported yet in WinUI 3, although it is a much-requested feature. A workaround is to create a helper class with a DependencyProperty in it that calls a change handler whenever the property is changed/set. The change handler can then create the required binding in code. Kudos to clemens who suggested something like this ages ago for UWP. Here is an example helper class:
Note that all of the DependencyProperties are of type string and that the target type can be any ancestor type of the control you are working with. For example, the
HeightBindingPathProperty
binding can be used with any FrameworkElement.Use the helper in a Style just as you would any Setter, like this:
I hope this helps.
今天早上,我对Winui的无意义限制将无尽的挫败感重定向到动机能量中,以制定通用解决方案。其他答案很棒,但这更可扩展:
用法:
顺便说一句,它起作用的事实 -
smartsetter.binding
给出了bindingbase
实例> bindingbase 实例,并且未实际评估绑定。在应用样式之前 - 告诉您Microsoft竭尽全力为setter
做相反的事情,从而导致了这个问题,原因是我无法开始理解。更新 - 我决定将此和另一个解决方法变成一个小项目。参见 https://github.com/peter0302/winui.redemption/empertion/
This morning I redirected my endless frustration with WinUI's senseless limitations into motivational energy to make a general purpose solution. Other answers are great but this is much more scalable:
Usage:
Incidentally, the fact that this works -
SmartSetter.Binding
is given aBindingBase
instance and the binding is not actually evaluated until the style is applied - tells you Microsoft went out of their way to do the opposite forSetter
, and thereby cause this problem, for reasons I cannot begin to fathom.Update - I decided to turn this and another workaround into a small project. See https://github.com/peter0302/WinUI.Redemption/