繁体   English   中英

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

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

所以我有这个项目,我有两个按钮和一个 ListView。 ListView 被分成它自己的UserControl和它自己的ViewModel ,其中包含一个ObservableCollection

我正在使用ContentPresenter来显示该控件,因为我将使用不同的视图。

目前,当我单击“ Log ”按钮时,它实际上确实将字符串添加到集合中,但视图不会更新。 每次我点击它时,它都会越来越多。 (我在private void AddItemOne()中放置了一个断点来检查它以证明它添加了项目。)

问题为什么当我点击“日志”按钮时我的视图没有更新,即使它正在添加项目。 如果我像这样对其进行硬编码,它确实会添加第一项。

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

主窗口.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>

主视图模型.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

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

日志视图模型.cs

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

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

杂项

App.xaml

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

和 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);
    }
}

从 LogView.xaml 中删除:

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

它创建LogViewModel的另一个实例,而不是使用您在MainViewModel中创建的实例。

您还应该使用绑定到CurrentView属性的ContentControl替换 MainWindow.xaml 中的UserControl

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM