C# 为什么在xaml中绑定ItemsSource时ui会更新,但不会';t在代码中分配时?
我正在尝试将具有数据模板的组合框绑定到由静态方法生成的类列表 分配C# 为什么在xaml中绑定ItemsSource时ui会更新,但不会';t在代码中分配时?,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我正在尝试将具有数据模板的组合框绑定到由静态方法生成的类列表 分配combo1.ItemsSource=Peoplelist时code behinde,comboboxui中的项目正在更新,但当我在xaml中进行绑定时:ItemsSource=“{binding Path=Peoplelist}”不是。我错过了什么 MainWindow.xaml.cs using System; using System.Collections.Generic; using System.Windows; n
combo1.ItemsSource=Peoplelist时
code behinde,combobox
ui中的项目正在更新,但当我在xaml中进行绑定时:ItemsSource=“{binding Path=Peoplelist}”
不是。我错过了什么
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",
});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Windows;
命名空间WpfTest
{
公共部分类主窗口:窗口
{
公众名单;
公共主窗口()
{
Peoplelist=新列表();
人员。填充(人员列表);
初始化组件();
//combo1.ItemsSource=Peoplelist;//正在工作
}
}
公营阶层人士
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公众人士({}
公众人物(String _FirstName,String _FamilyName)
{
FirstName=_FirstName;
姓氏=_FamilyName;
}
公共静态空白填充(列表lst)
{
一、新增(新增人员)
{
FirstName=“Jon”,
LastName=“乔纳森”,
});
一、新增(新增人员)
{
FirstName=“Mark”,
LastName=“Markthan”,
});
一、新增(新增人员)
{
FirstName=“Spence”,
LastName=“斯宾塞”,
});
}
}
}
main window.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数据绑定只适用于公共属性 所以你的主窗口类应该是
public partial class MainWindow : Window
{
public List<People> Peoplelist { get; set; }
public MainWindow()
{
Peoplelist = new List<People>();
People.Populate(Peoplelist);
DataContext = this;
InitializeComponent();
}
}
公共部分类主窗口:窗口
{
公共列表Peoplelist{get;set;}
公共主窗口()
{
Peoplelist=新列表();
人员。填充(人员列表);
DataContext=this;
初始化组件();
}
}
您的代码有两个问题。
第一:您没有在xaml或代码隐藏中指定数据上下文,因此它不知道要绑定到的数据的位置。
第二:您的Peoplelist应该是一个属性,因为WPF数据绑定只适用于公共属性
所以你的主窗口类应该是
public partial class MainWindow : Window
{
public List<People> Peoplelist { get; set; }
public MainWindow()
{
Peoplelist = new List<People>();
People.Populate(Peoplelist);
DataContext = this;
InitializeComponent();
}
}
公共部分类主窗口:窗口
{
公共列表Peoplelist{get;set;}
公共主窗口()
{
Peoplelist=新列表();
人员。填充(人员列表);
DataContext=this;
初始化组件();
}
}
Peoplelist也应该是只读属性。否则,它应该发出更改通知。将其声明为public List Peoplelist{get;}=new List()
或更好的公共ObservableCollection Peoplelist{get;}=new ObservableCollection()
在以后添加或删除元素时会更新UI。你完全正确。对不起,我忘了提那件事。这让我很难理解,因为我寻找了一种修改最少的方法来修复代码。@Clemens谢谢你是的,因为你说它必须是灵活的,当然我还必须改变填充方法:public static void populate(observeCollection lst)
@johan472更灵活:public static void populate(ICollection lst)
Peoplelist也应该是只读属性。否则,它应该发出更改通知。将其声明为public List Peoplelist{get;}=new List()
或更好的公共ObservableCollection Peoplelist{get;}=new ObservableCollection()
在以后添加或删除元素时会更新UI。你完全正确。对不起,我忘了提那件事。这让我很难理解,因为我寻找了一种修改最少的方法来修复代码。@Clemens谢谢你是的,因为你说它必须是灵活的,当然我还必须改变填充方法:public static void populate(observeCollection lst)
@johan472更灵活:public static void populate(ICollection lst)
注意combo1.ItemsSource=Peoplelist
不是绑定,只是一个普通的赋值。最好使用InterfaceINotifyPropertyChanged
。会让生活更轻松。注意combo1.ItemsSource=Peoplelist
不是绑定,只是一个普通的赋值。最好使用InterfaceINotifyPropertyChanged
。会让生活更轻松。