C# WPF未刷新动态创建的列表框
我有问题,下面的代码工作正常,它创建了一个新的列表框控件和一些项目。但是,当我想更改它的某些项目(例如,将新项目添加到C# WPF未刷新动态创建的列表框,c#,wpf,xaml,data-binding,listbox,C#,Wpf,Xaml,Data Binding,Listbox,我有问题,下面的代码工作正常,它创建了一个新的列表框控件和一些项目。但是,当我想更改它的某些项目(例如,将新项目添加到Title属性)时,列表框不会被刷新。为什么? XAML .CS 公共类MyDataItem:DependencyObject,INotifyPropertyChanged { 私有void NotifyPropertyChanged(字符串信息) { if(PropertyChanged!=null) { PropertyChanged(此,新PropertyChanged
Title
属性)时,列表框不会被刷新。为什么?
XAML
.CS
公共类MyDataItem:DependencyObject,INotifyPropertyChanged
{
私有void NotifyPropertyChanged(字符串信息)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(信息));
}
}
公开名单标题
{
得到
{
以列表形式返回GetValue(TitleProperty);
}
设置
{
设置值(标题属性、值);
通知财产变更(“所有权”);
}
}
公共静态只读从属属性TitleProperty=
DependencyProperty.Register(“标题”、类型(列表)、类型(MyDataItem)、新UIPropertyMetadata(新列表());
}
私有可观察收集数据项;
公共可观测收集数据项
{
获取{return dataItems;}
}
使用可观察收集
而不是列表
ObservableCollection
实现INotifyCollectionChanged
接口,允许它通知绑定已添加新项。使用ObservableCollection
而不是列表ObservableCollection
实现了INotifyCollectionChanged
接口,允许它通知绑定已添加了新项。依赖属性的CLR包装中除了对GetValue和SetValue的调用之外,应该没有任何内容。DependencyProperties有自己的更改通知,如果属性发生更改,则不一定会调用CLR setter代码(这就是为什么其中不应有任何内容)。此外,元数据应初始化为null
,否则所有数据项共享同一个集合实例。如果未设置其他值,请在ctor中创建一个实例(如果需要初始化)。可能需要查看。dependecy属性的CLR包装中除了对GetValue和SetValue的调用之外,应该没有任何内容。DependencyProperties有自己的更改通知,如果属性发生更改,则不一定会调用CLR setter代码(这就是为什么其中不应有任何内容)。此外,元数据应初始化为null
,否则所有数据项共享同一个集合实例。如果未设置其他值,请在ctor中创建一个实例(如果需要初始化)。我可能想看看这个。
<DataTemplate x:Key="myRes">
<Grid Background="White" Height="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Width="15" Height="18" Source="D:\separator.png"></Image>
<ListBox VerticalAlignment="Top" SelectionChanged="ListBox_SelectionChanged" Width="200" Height="300" Grid.Column="1" ItemsSource="{Binding Title}" />
</Grid>
</DataTemplate>
<ItemsControl VerticalAlignment="Top" Margin="5 0 0 0" Height="350" Grid.Column="1" Grid.Row="0"
ItemsSource="{Binding ElementName=mainWindow, Path=DataItems}"
ItemTemplate="{StaticResource myRes}">
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
public class MyDataItem : DependencyObject, INotifyPropertyChanged
{
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public List<string> Title
{
get
{
return GetValue(TitleProperty) as List<string>;
}
set
{
SetValue(TitleProperty, value);
NotifyPropertyChanged("Title");
}
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(List<string>), typeof(MyDataItem), new UIPropertyMetadata(new List<string>()));
}
private ObservableCollection<MyDataItem> dataItems;
public ObservableCollection<MyDataItem> DataItems
{
get { return dataItems; }
}