WPF Combox 数据已更新但 UI 未更新 - 更新代码 [英] WPF Combox data updated but UI not updated - Updated code
问题描述
代码如下:
MainWindow.xaml
<ComboBox Grid.Column="1" Margin="2" VerticalContentAlignment="Center" ItemsSource="{Binding Path=LowDLane, Mode=OneWay}"
SelectedIndex="{Binding Path=CurrentLowDLaneIndex, Mode=TwoWay, FallbackValue=0}"
DropDownOpened="onLowDLaneDropDownOpened"
SelectionChanged="onLowDLaneChanged">
</ComboBox>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow(ViewModel model)
{
InitializeComponent();
this.DataContext = model;
}
private void onLowDLaneDropDownOpened(object aSender, EventArgs aE)
{
((ViewModel)this.DataContext).openedDropDown();
}
}
ViewModel.cs 更新
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChangedHandler;
public List<string> LowDLane
{
get { return mLowDLane; }
set
{
mLowDLane = value;
PropertyChangedHandler.raise(this, ()=> LowDLane);
}
}
public void openedDropDown()
{
LowDLane = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8"};
}
}
在其他文件中定义了 PropertyChangedEventHandler:
In other file PropertyChangedEventHandler is defined:
namespace System.ComponentModel
{
public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);
}
表示将处理 System.ComponentModel.INotifyPropertyChanged.PropertyChanged 的方法更改组件上的属性时引发的事件.
Represents the method that will handle the System.ComponentModel.INotifyPropertyChanged.PropertyChanged event raised when a property is changed on a component.
PropertyChangedEventHandler 在其他地方运行良好,所以我不认为这是这里的问题.
PropertyChangedEventHandler works well in other places so I don't think this is the problem here.
我在其他文件中创建了 ViewModel
对象,并将其传递给 MainWindow
.当我运行应用程序时,我可以看到 LowDLane
属性已更新,但 UI 未更新.
I created ViewModel
object in other file, and passed it to MainWindow
.
When I run the application, I can see the LowDLane
property is updated, but the UI is not updated.
我看过很多类似的问题,但没有一个能解决我的问题.有人可以帮忙吗?
I have looked many similar questions, but none of them solved my issue. Can someone help?
推荐答案
将 UpdateSourceTrigger 属性添加到组合框 itemSource 绑定并使 Mode 为 TwoWay.
Add UpdateSourceTrigger property to Combo box itemSource binding and make Mode to TwoWay.
ItemsSource="{Binding Path=LowDLane, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
如果这能解决问题,请告诉我.
Let me know if this solves the issue.
这篇关于WPF Combox 数据已更新但 UI 未更新 - 更新代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!