【问题标题】:WPF Binding: How to databind to Grid?WPF 绑定:如何将数据绑定到网格?
【发布时间】:2011-05-28 11:09:37
【问题描述】:

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

最后,我写了ReorderWindow WPF window 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 以及如何使其使用我的数据模板?谢谢。

【问题讨论】:

  • 您是否在您的 Account 和 ReorderWindowController 类上实现了 INotifyPropertyChanged?数据绑定依赖 System.ComponentModel.INotifyPropertyChanged 接收更改通知,这会导致绑定检索值。
  • 是的,我做到了。虽然我面临的问题与更改通知无关,但感谢您的提醒。

标签: wpf data-binding grid binding


【解决方案1】:

使用 ContentPresenter 代替 Grid:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-31
    • 2017-05-14
    • 2011-10-26
    • 2011-09-28
    • 2010-11-15
    • 1970-01-01
    相关资源
    最近更新 更多