【问题标题】:Why won't my databound WPF ListBox show rows?为什么我的数据绑定 WPF ListBox 不显示行?
【发布时间】:2014-03-05 20:22:38
【问题描述】:

我有以下 WPF XAML:

<Window x:Class="ListBoxTesting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ListBox Name="ListBoxOne" Grid.Row="0" ItemsSource="{Binding ListOne}" DisplayMemberPath="NameOne"/>
        <ListBox Name="ListBoxTwo" Grid.Row="1" ItemsSource="{Binding ListTwo}" DisplayMemberPath="NameTwo"/>
    </Grid>
</Window>

使用此代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListBoxTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public class TypeOne
        {
            public string NameOne { get; set; }

            public TypeOne(string name)
            {
                NameOne = name;
            }
        }

        public class TypeTwo
        {
            public string NameTwo { get; set; }

            public TypeTwo(string name)
            {
                NameTwo = name;
            }
        }

        public ObservableCollection<TypeOne> ListOne { get; set; }
        public ObservableCollection<TypeTwo> ListTwo { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            ListOne = new ObservableCollection<TypeOne>();
            ListOne.Add(new TypeOne("Mike"));
            ListOne.Add(new TypeOne("Bobby"));
            ListOne.Add(new TypeOne("Joe"));
            ListTwo = new ObservableCollection<TypeTwo>();
            ListTwo.Add(new TypeTwo("Mike"));
            ListTwo.Add(new TypeTwo("Bobby"));
            ListTwo.Add(new TypeTwo("Joe"));
        }
    }
}

但由于某种原因,一旦我在调试模式下启动项目,我的 UI 中就看不到任何行。任何想法为什么?

【问题讨论】:

    标签: c# .net wpf xaml binding


    【解决方案1】:

    您需要将DataContext 设置为代码隐藏以解析您的绑定:

    可能在代码后面

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this; <--- HERE
            .....
        }
    

    XAML 中的或:

    <Window x:Class="ListBoxTesting.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"> <-- HERE
    

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2020-05-25
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多