数据绑定到控件

发布于 2024-12-13 15:19:07 字数 1237 浏览 0 评论 0原文

我将 Silverlight 与 MVVM 一起使用。

我有一个简单的 LoginControl 绑定到 LoginControlViewModelLoginControl 已创建并添加到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2024-12-20 15:19:07

啊,我看到您的属性位于与控件的 DataContext 不同的 ViewModel 上。

我过去所做的是将 LoginControl 放在另一个容器中(例如 BorderStackPanel),然后您可以绑定 <该面板对您的财产的 code>Visibility。这样,LoginControl 的 DataContext 保留 LoginControlViewModel 并且可见性绑定很简单。

编辑:Silverlight 不支持以下方法。

或者,您可以在 LoginControl 上进行复杂的绑定,查看其父级 DataContext

Visibility="{Binding Path=DataContext.LoginVisible, 
         RelativeSource={RelativeSource FindAncestor, AncestorType=MasterPage},
         Converter={StaticResource BoolConverter}}"

您的属性是否名为 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 a Border or StackPanel) and then you can bind the Visibility of that panel to your property. That way the DataContext of the LoginControl stays the LoginControlViewModel 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 parents DataContext:

Visibility="{Binding Path=DataContext.LoginVisible, 
         RelativeSource={RelativeSource FindAncestor, AncestorType=MasterPage},
         Converter={StaticResource BoolConverter}}"

Also isn't your property called LoginVisible not LoginControlVisibility as you have in your binding? Or was that a typo just in the question?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文