简体   繁体   English

为什么将项目添加到 ObservableCollection 时我的视图没有更新

[英]Why doesn't my view update when adding items to the ObservableCollection

So I have this project where I have two buttons and a ListView.所以我有这个项目,我有两个按钮和一个 ListView。 The ListView is separated into it's own UserControl with it's own ViewModel which contains a ObservableCollection . ListView 被分成它自己的UserControl和它自己的ViewModel ,其中包含一个ObservableCollection

I'm using a ContentPresenter to display that control because I will be using different views.我正在使用ContentPresenter来显示该控件,因为我将使用不同的视图。

Currently, when I'm clicking the Log button, it does in fact add the string to the collection, but the view doesn't update.目前,当我单击“ Log ”按钮时,它实际上确实将字符串添加到集合中,但视图不会更新。 And it keeps adding more and more everytime I click on it.每次我点击它时,它都会越来越多。 (I've put a breakpoint inside private void AddItemOne() to inspect it to prove that it adds items.) (我在private void AddItemOne()中放置了一个断点来检查它以证明它添加了项目。)

Question Why doesn't my view update when I click the "Log" button even though it's adding items.问题为什么当我点击“日志”按钮时我的视图没有更新,即使它正在添加项目。 It does add the first item if I hardcode it like this.如果我像这样对其进行硬编码,它确实会添加第一项。

public LogViewModel()
{
    Logs = new ObservableCollection<string>();
    Logs.Add("Test");
}

MainWindow.xaml主窗口.xaml

<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<Grid>
    <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

    <Button Grid.Row="0" Grid.Column="0" Height="25" Content="Log" 
            Command="{Binding AddItemOneCommand}"/>
    <Button Grid.Row="0" Grid.Column="1" Height="25" Content="Other"
            Command="{Binding AddItemTwoCommand}"/>

    <UserControl Content="{Binding CurrentView}" Grid.Row="1" Grid.ColumnSpan="2"/>
</Grid>

MainViewModel.cs主视图模型.cs

class MainViewModel
{
    public RelayCommand AddItemOneCommand { get; set; }
    public RelayCommand AddItemTwoCommand { get; set; }


    private object _currentView;

    public object CurrentView
    {
        get { return _currentView; }
        set
        {
            _currentView = value;
        }
    }


    /* ViewModels */
    public LogViewModel LogViewModel { get; set; }

    public MainViewModel()
    {
        AddItemOneCommand = new RelayCommand(o => AddItemOne());
        AddItemTwoCommand = new RelayCommand(o => AddItemTwo());

        LogViewModel = new LogViewModel();

        _currentView = LogViewModel;


    }

    private void AddItemOne()
    {
        LogViewModel.Logs.Add("Test");
    }


    private void AddItemTwo()
    {
        LogViewModel.Logs.Add("Test");
    }
}

LogView.xaml LogView.xaml

<UserControl.DataContext>
    <local:LogViewModel/>
</UserControl.DataContext>
<Grid>
    <ListView ItemsSource="{Binding Logs}" Background="Gray"/>
</Grid>

LogViewModel.cs日志视图模型.cs

class LogViewModel
{
    public ObservableCollection<string> Logs { get; set; }

    public LogViewModel()
    {
        Logs = new ObservableCollection<string>();
    }
}

Misc杂项

App.xaml App.xaml

<Application.Resources>
    <DataTemplate DataType="{x:Type local:LogViewModel}">
        <local:LogView/>
    </DataTemplate>
</Application.Resources>

And the RelayCommand和 RelayCommand

public class RelayCommand : ICommand
{
    private Action<object> execute;
    private Func<object, bool> canExecute;

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
    {
        this.execute = execute;
        this.canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return this.canExecute == null || this.canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        this.execute(parameter);
    }
}

Remove this from LogView.xaml:从 LogView.xaml 中删除:

<UserControl.DataContext>
    <local:LogViewModel/>
</UserControl.DataContext>

It creates another instance of the LogViewModel instead of using the one that you create in the MainViewModel .它创建LogViewModel的另一个实例,而不是使用您在MainViewModel中创建的实例。

You should also replace the UserControl in MainWindow.xaml with a ContentControl that binds to the CurrentView property:您还应该使用绑定到CurrentView属性的ContentControl替换 MainWindow.xaml 中的UserControl

<ContentControl Content="{Binding CurrentView}" Grid.Row="1" Grid.ColumnSpan="2" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将项目添加到DataBound集合时,为什么视图没有更新? - Why doesn't my view update when adding items to my DataBound collection? 将项目添加到ObservableCollection不会更改ListView - Adding items to ObservableCollection doesn't change the ListView 向ObservableCollection添加元素不会更新UI - Adding elements to ObservableCollection doesn't update UI 为什么我的ObservableCollection序列化不起作用? - Why my ObservableCollection serialization doesn't work? 为什么我的DataGrid带有ObservableCollection <T> 当我从集合中添加或删除项目时,显然不会刷新? - Why is my DataGrid with a ObservableCollection<T> apparently not refreshing when I add or remove items from the collection? 绑定到ObservableCollection的ListView <T> 添加到媒体资源时不更新 - ListView bound to ObservableCollection<T> Doesn't update when Added to property ObservableCollection不会更新ListView - ObservableCollection doesn't update ListView 当ObservableCollection绑定更改时,Listview不会更新 - Listview doesn't update when the ObservableCollection its bound to changes 当ObservableCollection中的项目更新时,ItemsControl视图不会更新 - ItemsControl view doesn't updated when an item in ObservableCollection is updated 为什么我的ObservableCollection不会 <MenuItem> 更新RaisePropertyChanged? - Why won't my ObservableCollection<MenuItem> update on RaisePropertyChanged?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM