【问题标题】:WPF: DataBinding a ListView in a UserControl shows no itemsWPF:在用户控件中对 ListView 进行数据绑定不显示任何项目
【发布时间】:2011-04-20 14:43:24
【问题描述】:

我很确定这是一个 UserControl DataContext 问题,但我没有看到它:

这是我的 XAML:

<UserControl x:Class="WFT.Controls.DetailsBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wft="clr-namespace:WFT.Controls" >

    <wft:CaptionedBox Caption="Details" Margin="1" >
        <ListView ItemsSource="{Binding Map}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Key}"  />
                    <GridViewColumn DisplayMemberBinding="{Binding Value}" />
                </GridView>
            </ListView.View>
        </ListView>
    </wft:CaptionedBox>
</UserControl>

这是代码隐藏:

public partial class DetailsBox : UserControl
{
    ObservableCollection<KeyValuePair<string, string>> m_Map =
        new ObservableCollection<KeyValuePair<string, string>>( );

    public ObservableCollection<KeyValuePair<string, string>> Map
    { get { return m_Map; } }

    public DetailsBox( )
    {
        InitializeComponent( );
        DataContext = this;
    }

    public void Initialize( List<string> map )
    {
        IEnumerable<int> range = Enumerable.Range( 0, map.Count );

        m_Map = new ObservableCollection<KeyValuePair<string, string>>(
            range.Where( r => 0 == r % 2 && map[ r + 1 ].Trim( ) != "N/A" )
            .Select( r => new KeyValuePair<string, string>( map[ r ], map[ r + 1 ] ) ).ToList( ) );

    }
}

在运行时,Map 有八个项目,但 ListView 中没有显示任何内容。在一个独立的测试应用程序中,它与DataContext="{Binding RelativeSource={RelativeSource Self}}" 一起工作,但作为一个用户控件,它不起作用。正如您在上面看到的,我什至尝试在构造函数中设置DataContext = this

谢谢!

【问题讨论】:

  • 你的ListView的DataContext设置了吗?乍一看,一切都很好,但我看不出 DataContext 是如何设置的。
  • 我编辑了问题以显示 DataContext。我从教程中复制了这个......有没有办法指定这是针对 ListView 本身的?事实上,它是在 XAML 标头中定义的。
  • 我很确定问题出在 DataContext 中;我的 xaml 是一个用户控件; RelativeSource 绑定在窗口中对我有用,但在 UserControl 中不起作用(已编辑原始问题)。我尝试过的某些事情会立即关闭 VS2008!

标签: data-binding .net-3.5 binding


【解决方案1】:

我从来没有弄清楚如何在 XAML 中设置 DataContext,但我通过执行以下操作得到了工作:

在 XAML 中,我将 x:Name="listview" 添加到 ListView。

然后,在后面的代码中,我将属性 Map 转换为 DependencyProperty

public ObservableCollection<KeyValuePair<string, string>> Map
{
    get { return (ObservableCollection<KeyValuePair<string, string>>)GetValue( MapProperty ); }
    set { SetValue( MapProperty, value ); }
} 

public static DependencyProperty MapProperty = DependencyProperty.Register( "Map",
     typeof( ObservableCollection<KeyValuePair<string, string>> ), 
     typeof( DetailsBox ),
     new PropertyMetadata( new ObservableCollection<KeyValuePair<string, string>>( ) ) );

从构造函数中移除DataContext = this;,并在加载地图后将listview.DataContext = Map;添加到Initialize。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    相关资源
    最近更新 更多