gpt4 book ai didi

WPF 绑定(bind) : How to databind to Grid?

转载 作者:行者123 更新时间:2023-12-04 15:44:15 25 4
gpt4 key购买 nike

我创建了一个类Account .
接下来,我创建了另一个类ReorderWindowController它有一个字段/属性 SelectedAccount类型 Account .

最后,我写了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没有显示任何东西。为什么?如何使对象数据绑定(bind)到 Grid以及如何让它使用我的数据模板?谢谢。

最佳答案

而不是 Grid 使用 ContentPresenter 像这样:

<Grid>         
<ContentPresenter
Name="AccountGrid"
Content="{Binding Source={StaticResource ResourceKey=WindowController}, Path=SelectedAccount}">
</ContentPresenter>
</Grid>

关于WPF 绑定(bind) : How to databind to Grid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4493445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com