ObservableCollection绑定UserControl的ListView后不更新,求排查原因
Ah, I get it—you've hit a common pitfall when creating WPF user controls. Let's break down what's happening and how to fix it:
The Root Cause
When you set DataContext = new MyViewModel() directly in the user control's code-behind, you're creating a completely separate instance of MyViewModel inside the user control. Here's the disconnect:
- Your page's button click is adding items to the
ObservableCollection<MissingValue>in the page's ownMyViewModelinstance. - But your user control's ListView is bound to the collection in its internal, isolated
MyViewModel—one that never receives the button's update commands.
The two ViewModels are totally unlinked, so changes to the page's collection don't reflect in the user control's ListView at all.
The Fix: Ditch the Hardcoded DataContext
Instead of forcing a new ViewModel into the user control, you need to keep it connected to the page's existing ViewModel. The cleanest way to do this is with a dependency property for the items source:
Step 1: Add a Dependency Property to Your User Control
Replace the hardcoded DataContext in MissingValuesListView's code-behind with this:
public partial class MissingValuesListView : UserControl { public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register( nameof(ItemsSource), typeof(ObservableCollection<MissingValue>), typeof(MissingValuesListView), new PropertyMetadata(null)); public ObservableCollection<MissingValue> ItemsSource { get => (ObservableCollection<MissingValue>)GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); } public MissingValuesListView() { InitializeComponent(); // Delete this line entirely: DataContext = new MyViewModel(); } }
Step 2: Bind the User Control's ListView to the Dependency Property
Update your user control's XAML to bind the ListView to the new property (using RelativeSource to target the user control itself):
<UserControl x:Class="YourNamespace.MissingValuesListView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ListView ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=UserControl}}"> <!-- Keep your existing item template here --> </ListView> </UserControl>
Step 3: Connect the User Control to the Page's ViewModel
In your page's XAML, bind the user control's ItemsSource to your page ViewModel's collection:
<local:MissingValuesListView ItemsSource="{Binding YourCollectionName}" />
Now when your button adds items to the page's ViewModel collection, the user control's ListView will update immediately—since both are bound to the same collection instance.
Why This Works
Dependency properties in WPF are designed for proper data binding and change notification. By using one instead of a hardcoded ViewModel, you ensure the user control inherits the page's data context flow, keeping everything synchronized.
内容的提问来源于stack exchange,提问作者Marija Rakic

