我在学习如何使用WPF绑定和MVVM体系结构。我遇到了依赖属性的一些麻烦。我试图通过将它绑定到DataContext中的DependencyProperty来控制视图上项目的可见性,但它不起作用。无论我如何在下面的视图模型的构造函数中设置GridVisible
值,在运行代码时它总是显示为可见。WPF数据绑定体系结构问题
任何人都可以看到我要去哪里错了吗?
C#代码(视图模型):
public class MyViewModel : DependencyObject
{
public MyViewModel()
{
GridVisible = false;
}
public static readonly DependencyProperty GridVisibleProperty =
DependencyProperty.Register(
"GridVisible",
typeof(bool),
typeof(MyViewModel),
new PropertyMetadata(false,
new PropertyChangedCallback(GridVisibleChangedCallback)));
public bool GridVisible
{
get { return (bool)GetValue(GridVisibleProperty); }
set { SetValue(GridVisibleProperty, value); }
}
protected static void GridVisibleChangedCallback(
DependencyObject source,
DependencyPropertyChangedEventArgs e)
{
// Do other stuff in response to the data change.
}
}
XAML代码(查看):
<UserControl ... >
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
</UserControl.Resources>
<UserControl.DataContext>
<local:MyViewModel x:Name="myViewModel" />
</UserControl.DataContext>
<Grid x:Name="_myGrid"
Visibility="{Binding Path=GridVisible,
ElementName=myViewModel,
Converter={StaticResource BoolToVisConverter}}">
<!-- Other elements in here -->
</Grid>
</UserControl>
我在几个教程在网上看了,似乎像我正确地遵循我在那里找到的内容。有任何想法吗?谢谢!
其实我把你的代码复制出来,发现它与包含和不包含ElementName的绑定一起工作。你在使用usercontrol(即一个窗口或其他用户控件)?也许在那里有什么问题(你没有包含代码)。 – thornhill 2011-04-29 21:51:28
我删除了ElementName,它仍然不起作用。看起来你是对的 - 它可能与我的代码的另一部分有关,而不是上面发布的消毒版本。不幸的是,我不能发布实际的代码,因为它是为了工作。 – RobotNerd 2011-04-29 21:55:34
在这种情况下,当您运行应用程序并打开包含此用户控件的窗口时,请在VisualStudio的“输出”窗口中查找与此文本类似的任何错误。 “BindingExpression path error:'GridVisible'property not found on 'object ......“ 该消息的内容可能会帮助您进一步调试。 抱歉,我无法帮助你更多。祝你好运。 – thornhill 2011-04-30 08:06:54