WPF 绑定:如何将数据绑定到网格?

发布于 2024-10-08 16:40:25 字数 1129 浏览 0 评论 0原文

我创建了一个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 技术交流群。

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

发布评论

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

评论(1

撕心裂肺的伤痛 2024-10-15 16:40:25

使用 ContentPresenter 代替 Grid,如下所示:

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

Instead of a Grid use a ContentPresenter like this:

<Grid>         
<ContentPresenter
Name="AccountGrid"              
Content="{Binding Source={StaticResource ResourceKey=WindowController},                           Path=SelectedAccount}">  
   </ContentPresenter>
</Grid> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文