WPF 绑定:如何将数据绑定到网格?
我创建了一个Account
类。
接下来,我创建了另一个类 ReorderWindowController
,它具有 Account
类型的字段/属性 SelectedAccount
。
最后,我编写了 ReorderWindow
WPF 窗口 xaml 文件:
<Window ...
<Window.Resources>
<contollers:ReorderWindowController x:Key="WindowController" />
<DataTemplate DataType="{x:Type entities:Account}">
<Grid Width="140" Height="50" Margin="5">
<TextBlock Text="Some awesome text" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text="Even more awesome text" />
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid
Name="AccountGrid"
DataContext="{Binding Source={StaticResource ResourceKey=WindowController},
Path=SelectedAccount}">
</Grid>
</Grid>
</Window>
当我运行代码时,AccountGrid
没有显示任何内容。为什么?如何将对象数据绑定到 Grid
以及如何使其使用我的数据模板?谢谢。
I have created a class Account
.
Next, I have created another class ReorderWindowController
which has a field/property SelectedAccount
of type Account
.
Finally, I have written ReorderWindow
WPF window xaml file:
<Window ...
<Window.Resources>
<contollers:ReorderWindowController x:Key="WindowController" />
<DataTemplate DataType="{x:Type entities:Account}">
<Grid Width="140" Height="50" Margin="5">
<TextBlock Text="Some awesome text" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text="Even more awesome text" />
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid
Name="AccountGrid"
DataContext="{Binding Source={StaticResource ResourceKey=WindowController},
Path=SelectedAccount}">
</Grid>
</Grid>
</Window>
When I run my code, AccountGrid
is not showing anything. Why? How do I make object data bind to the Grid
and how do I make it use my data template? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ContentPresenter 代替 Grid,如下所示:
Instead of a Grid use a ContentPresenter like this: