我在WPF应用程序中使用.Net 4.6.1。我们有一个ObservableCollection
,我们想将集合绑定到一个列表视图,该列表视图需要按字母顺序排序并根据集合中的新项目自动更新。
我使用CollectionViewSource
,并在StackOverflow上搜索了此处以进行更新,我需要使用INotifyPropertyChanged
。但是由于某种原因,它不起作用:(。
对象类:
using System.Text;
using System.Threading.Tasks;
namespace ObservableCollection
{
public class AlarmTypes : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public AlarmTypes(string _name)
{
this.name = _name;
}
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged("Name");
}
}
protected void OnPropertyChanged(string prop)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(prop));
}
}
}
}
XAML文件
<Window x:Class="ObservableCollection.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"
xmlns:local="clr-namespace:ObservableCollection"
xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.Resources>
<CollectionViewSource x:Name="CollectionViewSource" x:Key="src" Source="{Binding alarmTypes }" IsLiveFilteringRequested="True">
<CollectionViewSource.LiveSortingProperties>
<clr:String>Name</clr:String>
</CollectionViewSource.LiveSortingProperties>
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ListView x:Name="lstAlarmTypes" HorizontalAlignment="Left" Height="319" Margin="585,48,0,0" VerticalAlignment="Top" Width="157" ItemsSource="{Binding Source={StaticResource src}}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Name="textBox1"/>
<Button Content="Add item" HorizontalAlignment="Left" Margin="173,51,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<ListView x:Name="LstNames2" HorizontalAlignment="Left" Height="301" Margin="339,66,0,0" VerticalAlignment="Top" Width="180">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
XAML.cs
后面的代码:
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 ObservableCollection
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<AlarmTypes> alarmTypes = new ObservableCollection<AlarmTypes>();
private ObservableCollection<AlarmTypes> sortedTypes = new ObservableCollection<AlarmTypes>();
public MainWindow()
{
InitializeComponent();
LstNames2.ItemsSource = alarmTypes;
var alarmType = new AlarmTypes("inbraak");
alarmTypes.Add(alarmType);
var alarmType2 = new AlarmTypes("alarm");
//alarmType.Name = textBox1.Text;
alarmTypes.Add(alarmType2);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var alarmType = new AlarmTypes(textBox1.Text);
//alarmType.Name = textBox1.Text;
alarmTypes.Add(alarmType);
}
}
}
我不知道绑定是否起作用,或者是属性,还是我还需要其他东西。第一个列表仅具有与Observable集合的绑定,并且在从文本框中输入数据时起作用。但是具有collectionViewSource的第二个列表视图显然不起作用。有什么想法吗?
答案 0 :(得分:0)
您需要将alarmTypes
变成公共财产。