在 WPF 应用程序中使用 INotifyPropertyChanged
Using INotifyPropertyChanged in WPF applicatoin
我在 WPF 应用程序中使用 .Net 4.6.1。我们有一个 ObservableCollection
并且我们希望将集合绑定到一个列表视图,该列表视图需要按字母顺序排序并自动更新集合中的新项目。
我使用 CollectionViewSource
并在 Whosebug 上搜索更新我需要使用 INotifyPropertyChanged
。但由于某种原因它不起作用:(.
对象class:
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 的第二个列表视图显然不起作用。有什么想法遗漏了吗?
您需要将 alarmTypes
变成 public 属性。
我在 WPF 应用程序中使用 .Net 4.6.1。我们有一个 ObservableCollection
并且我们希望将集合绑定到一个列表视图,该列表视图需要按字母顺序排序并自动更新集合中的新项目。
我使用 CollectionViewSource
并在 Whosebug 上搜索更新我需要使用 INotifyPropertyChanged
。但由于某种原因它不起作用:(.
对象class:
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 的第二个列表视图显然不起作用。有什么想法遗漏了吗?
您需要将 alarmTypes
变成 public 属性。