簡體   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