- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
问题:
ItemsControl.ItemsSource
属性绑定(bind)到 ViewModel 中的 ObservableCollection
。 ObservableCollection
或从中删除时,您的 View 会按预期更新。 ObservableCollection
中某个项目的属性时, View 不会更新。 背景:
看来这是很多WPF开发者遇到的通病。已经被问过几次了:
Notify ObservableCollection when Item changes
ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)
ObservableCollection and Item PropertyChanged
我的实现:
我尝试在 Notify ObservableCollection when Item changes 中实现公认的解决方案.基本思想是在您的 MainWindowViewModel 中为 ObservableCollection
中的每个项目连接一个 PropertyChanged
处理程序。当一个项目的属性被更改时,事件处理程序将被调用,并且 View 会以某种方式更新。
我无法让实现工作。这是我的实现。
View 模型:
class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
项目 View 模型:
class EmployeeViewModel : ViewModelBase
{
private int _age;
private string _name;
public int Age
{
get { return _age; }
set
{
_age = value;
RaisePropertyChanged("Age");
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
public override string ToString()
{
return string.Format("{0} is {1} years old", Name, Age);
}
}
主窗口 View 模型:
class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<EmployeeViewModel> _collection;
public MainWindowViewModel()
{
_collection = new ObservableCollection<EmployeeViewModel>();
_collection.CollectionChanged += MyItemsSource_CollectionChanged;
AddEmployeeCommand = new DelegateCommand(() => AddEmployee());
IncrementEmployeeAgeCommand = new DelegateCommand(() => IncrementEmployeeAge());
}
public ObservableCollection<EmployeeViewModel> Employees
{
get { return _collection; }
}
public ICommand AddEmployeeCommand { get; set; }
public ICommand IncrementEmployeeAgeCommand { get; set; }
public void AddEmployee()
{
_collection.Add(new EmployeeViewModel()
{
Age = 1,
Name = "Random Joe",
});
}
public void IncrementEmployeeAge()
{
foreach (var item in _collection)
{
item.Age++;
}
}
private void MyItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
foreach (EmployeeViewModel item in e.NewItems)
item.PropertyChanged += ItemPropertyChanged;
if (e.OldItems != null)
foreach (EmployeeViewModel item in e.OldItems)
item.PropertyChanged -= ItemPropertyChanged;
}
private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
RaisePropertyChanged("Employees");
}
}
查看:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:d="clr-namespace:Iress.IosPlus.DynamicOE.Controls"
Title="MainWindow" Height="350" Width="350">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="0.7*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Button Command="{Binding AddEmployeeCommand}">Add Employee</Button>
<Button Command="{Binding IncrementEmployeeAgeCommand}">Increment Employee Age</Button>
</StackPanel>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=Employees[0]}"></TextBlock>
<ItemsControl Grid.Row="1" ItemsSource="{Binding Path=Employees}" BorderBrush="Red" BorderThickness="1"></ItemsControl>
</Grid>
</Grid>
我的结果:
为了验证我的实现,我创建了一个像这样的 View 。 TextBlock.Text
绑定(bind)到集合中的第一项。 ItemsControl
绑定(bind)到集合本身。
EmployeeViewModel
对象,并且 TextBlock
和 ItemsControl
都会按预期更新。 ItemsControl
将更新为另一个条目。太棒了!Age
属性递增 1。引发 PropertyChanged
事件。 ItemPropertyChanged
事件处理程序被调用。 Textblock
按预期更新。但是,ItemsControl
没有更新。 我的印象是,当 Employee.Age
根据 Notify ObservableCollection when Item changes 中的答案更改时,ItemsControl
也应该更新。 .
最佳答案
我使用 Snoop 找到了答案调试 XAML。
问题是您正在尝试绑定(bind)到 ToString() 方法并且不会引发 PropertyChanged 事件。如果您查看 XAML 绑定(bind),您会注意到 ObservableCollection 实际上正在发生变化。
现在查看每个项目控件及其在“文本”属性中的文本绑定(bind)。没有,只有文字。
要解决此问题,只需添加一个 ItemsControl ItemTemplate 和一个包含您希望显示的元素的 DataTemplate。
<ItemsControl Grid.Row="1" ItemsSource="{Binding Path=Employees, UpdateSourceTrigger=PropertyChanged}" BorderBrush="Red" BorderThickness="1" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat=" {0} is {1} years old">
<Binding Path="Name"/>
<Binding Path="Age"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我们现在对绑定(bind)开了绿灯。正在调用 RaisePropertyChanged。
哒哒!
关于c# - 当更新 ObservableCollection 中的项目时更新 ItemsControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28422800/
我正在尝试使用 tkinter 创建一个类似点击器的游戏作为练习。我对 tkinter 很陌生,所以如果问题非常基本,我深表歉意。我设置了一个按钮来添加点击次数,并且我还尝试设置自动点击功能。我的代码
我想以特定方式更新表 A 中的第 1 列:当列中的第三个字符是“_”时,我想插入前 2 个字符,如果第三个字符还有其他任何内容,我想保留它是。 例子: |col1|
我用 View 模型组装了一个简单的登录 fragment 。这是 fragment : class LoginFragment : Fragment() { companion object {
是否可以在 mySQL 中的创建表语句中编写更新语句?假设我们有两个不同的表。当我在一个表上插入某些内容时,我想更改另一表中的值。这在 mySQL 中可能吗 最佳答案 需要更多规范。 如果您有 2 个
我组合了一个简单的发布/订阅模式,以允许动态实例化和运行多个 JavaFX 类。这些类中的每一个(“订阅者”)都旨在可视化来自具有“发布者”角色的模型(在模型/ View / Controller 意
我正在使用 pygame 并在主循环的每个循环中更新到屏幕。我不明白的是,在我添加一个 for 循环查找事件之前,什么都不会更新,然后突然所有更新都发生了。这是为什么? def run(self
我是 React 的初学者;我知道 setState 是异步的,但我不明白为什么在示例笔中下方框下方的刷新不会立即完成,而仅在输入第二个字符后才更新。 Codepen:(已更新以链接到正确的笔) ht
我在 Java 程序中有两个选项卡。一个用于添加股票,另一个用于列出我创建的股票。当我在第二个选项卡中添加新项目时,我试图设法更新第一个选项卡中的项目列表。有任何想法吗? 我希望第一个选项卡显示我在第
我有一个 Activity A。加载 A 后,单击 A 中的按钮会在 A 的主页布局上添加 fragment F。现在一旦进入 F,如果我正在调用 getActivity().getSupportFr
下面提供的这段代码中的 friend 们,我想在从播放 Intent 恢复时刷新我的 TextView 。但是每当我尝试在 OnCreate 之外但在我的主类中定义我的 textview 时(在 st
我是 Postgres 的新手。我正在尝试使用 java/postgres jdbc 执行以下代码(适用于 MySQL): for (int i = 0; i < arr.size(); i++) {
目前,我有一个更新函数,可以更新一行,但如果我将其中一个框留空,而不是不更改该值,则该值将被删除。如果我只需要更新其中一个值,我想更新该值并将其他框留空。这可能吗? 目前我的代码是这样的。 最佳答案
我正在编写 JavaScript,它正在为一个项目计数到一定数量。数量可能在 100,000 左右,完成处理大约需要 10-15 秒。我希望脚本在用户调用页面时立即运行,并在脚本完成时进行重定向。是否
每当具有不同输入 ID 的另一个 selectInput 的值发生变化时,我需要更改一个具有自己输入 ID 的 selectInput 的值。 在 javascript 中,这将是 onchage,但
我正在尝试弄清楚如何在将视频上传到服务器时更新我的 UIProgressView。该视频是用户选择的视频,这是我上传到我的服务器的代码: NSMutableArray *array = [[NSM
我想在单击 h:commandButton 时更新 id="contents"的 div,但复杂的部分是 h:commandButton 位于 c:forEach Click Me
我目前正在构建一个读取数组并将其显示在 TableView 上的 UITableView。我不断地添加到数组中,我希望在 View 上有一个按钮,一旦单击它就会更新 UITableView。 如何从我
我在 StructuredProperty 中有一个 ComputedProperty,它在首次创建对象时不会更新。 当我创建对象时 address_components_ascii 没有被保存。该字
我知道 SCNPhysicsBody 在节点缩放时不会缩放,但我还没有找到解决此问题的好方法。我想缩放节点,然后在缩放后将 SCNPhysicsBody 更新为节点。 let box = SCNBox
我在自定义 UITableViewCell 中有一个 UILabel,当设备旋转时,它会调整大小。旋转后需要重新计算此标签中的文本,因为我要将其缩小到适当大小并在末尾附加一些文本。 例如数据模型有:“
我是一名优秀的程序员,十分优秀!