数据绑定到控件
我将 Silverlight 与 MVVM 一起使用。
我有一个简单的 LoginControl
绑定到 LoginControlViewModel
。 LoginControl
已创建并添加到我的 MasterPage
中。
当用户登录到我的应用程序时,通过按 LoginControl
上的登录按钮,LoginControlViewModel
会将事件发送回 MasterPageControlViewModel
。此时,MasterPageControlViewModel.LoginVisible
属性设置为 False
。
我遇到的问题是 MasterPageView 的 XAML,因为我不知道如何将 LoginControl 绑定到 MasterPageControlViewModel.LoginVisible 属性。下面的不行。
<Controls:Login x:Name="LoginControl" Style="{StaticResource LoginControlStyle}"
Visibility="{Binding LoginControlVisibility, Converter={StaticResource BoolConverter}}" />
输出窗口显示以下内容:
<块引用>System.Windows.Data 错误:BindingExpression 路径错误:在“Silverlight.Controls.LoginControlViewModel”“Silverlight.Controls.LoginControlViewModel”上找不到“LoginControlVisibility”属性 (HashCode=43749873)。 BindingExpression: Path='LoginControlVisibility' DataItem='Silverlight.Controls.LoginControlViewModel' (HashCode=43749873);目标元素是 'Controls.Login' (Name='LoginControl');目标属性是“Visibility”(类型“System.Windows.Visibility”)
知道如何解决这个问题吗?
I'm using Silverlight with the MVVM.
I have a simple LoginControl
bound to a LoginControlViewModel
. The LoginControl
is created and added to my MasterPage
.
When the user logs in to my app, by pressing the Login BUtton on the LoginControl
, the LoginControlViewModel
sends an event back to the MasterPageControlViewModel
. At this point the MasterPageControlViewModel.LoginVisible
property is set to False
.
The problem i am having is with XAML of the MasterPageView
, since I dont know how to bind the LoginControl to the MasterPageControlViewModel.LoginVisible
property. The below doesn't work.
<Controls:Login x:Name="LoginControl" Style="{StaticResource LoginControlStyle}"
Visibility="{Binding LoginControlVisibility, Converter={StaticResource BoolConverter}}" />
The output window states the following:
System.Windows.Data Error: BindingExpression path error: 'LoginControlVisibility' property not found on 'Silverlight.Controls.LoginControlViewModel' 'Silverlight.Controls.LoginControlViewModel' (HashCode=43749873). BindingExpression: Path='LoginControlVisibility' DataItem='Silverlight.Controls.LoginControlViewModel' (HashCode=43749873); target element is 'Controls.Login' (Name='LoginControl'); target property is 'Visibility' (type 'System.Windows.Visibility')
Any idea how to resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊,我看到您的属性位于与控件的 DataContext 不同的 ViewModel 上。
我过去所做的是将
LoginControl
放在另一个容器中(例如Border
或StackPanel
),然后您可以绑定 <该面板对您的财产的 code>Visibility。这样,LoginControl
的 DataContext 保留LoginControlViewModel
并且可见性绑定很简单。编辑:Silverlight 不支持以下方法。
或者,您可以在
LoginControl
上进行复杂的绑定,查看其父级DataContext
:您的属性是否名为
LoginVisible
,而不是LoginControlVisibility
正如您在绑定中所拥有的那样?或者这只是问题中的错字?Ah, and I see your property is on a different ViewModel than what the control has for it's DataContext.
What I've done in the past is place the
LoginControl
in another container (say aBorder
orStackPanel
) and then you can bind theVisibility
of that panel to your property. That way the DataContext of theLoginControl
stays theLoginControlViewModel
and the visibility binding is simple.Edit: Turns out below method is not supported in Silverlight.
Or you can have a complicated binding on the
LoginControl
that looks at its parentsDataContext
:Also isn't your property called
LoginVisible
notLoginControlVisibility
as you have in your binding? Or was that a typo just in the question?