WPF绑定:如何进行数据绑定到网格? [英] WPF Binding: How to databind to Grid?

查看:222
本文介绍了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> 

这篇关于WPF绑定:如何进行数据绑定到网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆