WPF绑定:如何进行数据绑定到网格?
问题描述:
我创建了一个类帐户
。结果
接下来,我创建了另一个类 ReorderWindowController
有类型的字段/属性 SelectedAccount
帐户
。
I have created a class Account
.
Next, I have created another class ReorderWindowController
which has a field/property SelectedAccount
of type Account
.
最后,我写了 ReorderWindow
WPF窗口的XAML文件:
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>
当我运行我的code, AccountGrid
没有显示任何东西。为什么?如何让我的对象数据绑定到电网
以及如何使其使用我的数据模板?谢谢。
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.
答
而不是一个网格的使用内容presenter是这样的:
Instead of a Grid use a ContentPresenter like this:
<Grid>
<ContentPresenter
Name="AccountGrid"
Content="{Binding Source={StaticResource ResourceKey=WindowController}, Path=SelectedAccount}">
</ContentPresenter>
</Grid>