为什么在xaml中绑定ItemsSource时ui更新,而在代码中分配时却不更新? [英] Why ui update when binding ItemsSource in xaml but doesn't when assigning it in code?
问题描述
我试图将具有 DataTemplate
的 combobox
绑定到静态方法生成的类列表 Populate()
。
I am trying to bind a combobox
having a DataTemplate
to a list of classes generated by a static methode Populate()
.
分配 combo1.ItemsSource = Peoplelist;
代码隐藏,ui中的 combobox
项目正在更新,但是当我在xaml中进行绑定时: ItemsSource = {Binding Path = Peoplelist}
不是。我缺少什么?
When assigning combo1.ItemsSource = Peoplelist;
code-behinde, combobox
items in ui are updating, but when I do a binding in xaml: ItemsSource="{Binding Path=Peoplelist}"
it is not. What I am missing?
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
namespace WpfTest
{
public partial class MainWindow : Window
{
public List<People> Peoplelist;
public MainWindow()
{
Peoplelist = new List<People>();
People.Populate(Peoplelist);
InitializeComponent();
//combo1.ItemsSource = Peoplelist; //working
}
}
public class People
{
public string FirstName { get; set; }
public string LastName { get; set; }
public People() { }
public People(String _FirstName, String _FamilyName)
{
FirstName = _FirstName;
LastName = _FamilyName;
}
public static void Populate(List<People> lst)
{
lst.Add(new People
{
FirstName = "Jon",
LastName = "Jonathan",
});
lst.Add(new People
{
FirstName = "Mark",
LastName = "Markthan",
});
lst.Add(new People
{
FirstName = "Spence",
LastName = "Spencer",
});
}
}
}
MainWindow.xaml
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ComboBox Name="combo1" VerticalAlignment="Center" HorizontalAlignment="Center" MinWidth="200"
SelectedValuePath="LastName" ItemsSource="{Binding Path=Peoplelist}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="--"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
推荐答案
您的代码有两点错误。
首先:您没有在xaml或后面的代码中指定数据上下文,因此它不知道要将数据绑定到何处。
第二:您的Peoplelist应该是一个属性,因为WPF数据绑定仅适用于公共属性。
There are two things wrong with your code. First: You didn't specify a data context in your xaml or code behind so it doesn't know where the data is that you want to bind to. Second: Your Peoplelist should be a property because WPF data binding only works with public properties.
因此您的MainWindow类应该是
So your MainWindow class should be
public partial class MainWindow : Window
{
public List<People> Peoplelist { get; set; }
public MainWindow()
{
Peoplelist = new List<People>();
People.Populate(Peoplelist);
DataContext = this;
InitializeComponent();
}
}
这篇关于为什么在xaml中绑定ItemsSource时ui更新,而在代码中分配时却不更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!