更新 ObservableCollection 中的值
Updating values in ObservableCollection
嘿,我有一个 ObservableCollection
,它由一个 class 和绑定到 listbox
.
的两个属性(字符串 = 用户和响应)组成
我想先在列表框中添加用户,然后添加:
for (int i = 0; i < ArrStrUser.Length; i++)
{
Users.Add(new User() { input = ArrStrUser[i].Trim() });
}
我想稍后添加对相应用户的回复。
如果我这样做,它们将被添加到 ObservableCollection 但不会在列表框中更新。
Users[i].response = strOutput.Trim().Replace(Environment.NewLine, " ");
ObservableCollecton
private ObservableCollection<Input> Users = new ObservableCollection<Input>();
Class:
public class Input
{
public string user{ get; set; }
public string response { get; set; }
}
XAML:
<ListBox x:Name="LBresponse" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource UserTemplate}" />
<DataTemplate x:Key="UserTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path= user}" Width="50"/>
<TextBlock Text="{Binding Path= response}" />
<Button Content="Delete" Click="DeleteUser_Clicked" HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
简单的解决方案
您的 Input
class 需要实现 INotifyPropertyChanged
接口并在更改 属性 的值时调用 PropertyChanged
事件以更新ListBox
。 ObservableCollection
只“关心”添加或删除项目,它不处理项目的 属性 更改。
尝试像这样编辑您的输入class:
public class Input : INotifyPropertyChanged
{
public string user{ get; set; }
private string _response;
public string Response{
get => _response;
set {
_response = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
现在更改 Response
属性 应该会更新 UI。
更好的解决方案
如果您也想在其他地方使用它,我还建议您将 INotifyPropertyChanged
实现分离到它自己的 class 中。或者更好的是,使用已有的库,例如 James Montemagno 的 mvvm-helpers nuget package。
这是 link 到 INotifyPropertyChanged
implementation from that library
你是这样使用它的:
public class Input : ObservableObject
{
public string user{ get; set; }
private string _response;
public string Response{
get => _response;
set => SetProperty(ref _response, value);
}
}
它还支持传入 OnChanged Action
和验证函数。
嘿,我有一个 ObservableCollection
,它由一个 class 和绑定到 listbox
.
我想先在列表框中添加用户,然后添加:
for (int i = 0; i < ArrStrUser.Length; i++)
{
Users.Add(new User() { input = ArrStrUser[i].Trim() });
}
我想稍后添加对相应用户的回复。 如果我这样做,它们将被添加到 ObservableCollection 但不会在列表框中更新。
Users[i].response = strOutput.Trim().Replace(Environment.NewLine, " ");
ObservableCollecton
private ObservableCollection<Input> Users = new ObservableCollection<Input>();
Class:
public class Input
{
public string user{ get; set; }
public string response { get; set; }
}
XAML:
<ListBox x:Name="LBresponse" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource UserTemplate}" />
<DataTemplate x:Key="UserTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path= user}" Width="50"/>
<TextBlock Text="{Binding Path= response}" />
<Button Content="Delete" Click="DeleteUser_Clicked" HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
简单的解决方案
您的 Input
class 需要实现 INotifyPropertyChanged
接口并在更改 属性 的值时调用 PropertyChanged
事件以更新ListBox
。 ObservableCollection
只“关心”添加或删除项目,它不处理项目的 属性 更改。
尝试像这样编辑您的输入class:
public class Input : INotifyPropertyChanged
{
public string user{ get; set; }
private string _response;
public string Response{
get => _response;
set {
_response = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
现在更改 Response
属性 应该会更新 UI。
更好的解决方案
如果您也想在其他地方使用它,我还建议您将 INotifyPropertyChanged
实现分离到它自己的 class 中。或者更好的是,使用已有的库,例如 James Montemagno 的 mvvm-helpers nuget package。
这是 link 到 INotifyPropertyChanged
implementation from that library
你是这样使用它的:
public class Input : ObservableObject
{
public string user{ get; set; }
private string _response;
public string Response{
get => _response;
set => SetProperty(ref _response, value);
}
}
它还支持传入 OnChanged Action
和验证函数。