Home > Mobile >  How to change list contents within another WPF window
How to change list contents within another WPF window

Time:11-30

I have two WPF windows, one with a list and another one which is supposed to change the contents of that list. How could I do that?

Many thanks for your time.

CodePudding user response:

Simplest example

enter image description here

The model:

using System;
using System.Collections.ObjectModel;

namespace WpfApp1
{
    internal class Model
    {
        public Model()
        {
            AddStuff = new RelayCommand(() => Collection.Add(DateTime.Now.ToString()));
        }

        public RelayCommand AddStuff { get; }

        public ObservableCollection<string> Collection { get; } = new();
    }
}

The command:

#nullable enable
using System;
using System.Windows.Input;

namespace WpfApp1
{
    public class RelayCommand : ICommand
    {
        private readonly Func<bool> _canExecute;
        private readonly Action _execute;

        public RelayCommand(Action execute) : this(execute, () => true)
        {
        }

        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object? parameter)
        {
            return _canExecute();
        }

        public void Execute(object? parameter)
        {
            _execute();
        }

        public event EventHandler? CanExecuteChanged;
    }
}

App.xaml: the model is shared

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="Window1.xaml">
    <Application.Resources>
        <local:Model x:Key="Model" />
    </Application.Resources>
</Application>

Window 1:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="WpfApp1.Window1"
    mc:Ignorable="d"
    Title="Window1" Height="450" Width="800" DataContext="{StaticResource Model}">
    <Grid>
        <ListBox ItemsSource="{Binding Collection}" />
    </Grid>
</Window>

Window 1:

using System.Windows;

namespace WpfApp1
{
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();
            Loaded  = onl oaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            new Window2().Show();
        }
    }
}

Window 2:

<Window x:Class="WpfApp1.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Window2" Height="450" Width="800" DataContext="{StaticResource Model}">
    <Grid>
        <Button Content="Whatever" Command="{Binding AddStuff}" />
    </Grid>
</Window>

Window 2:

namespace WpfApp1
{
    public partial class Window2
    {
        public Window2()
        {
            InitializeComponent();
        }
    }
}

CodePudding user response:

For example by keeping a reference to the window to be updated from the other window, or get the reference to that window using the Application.Current.Windows property:

var listBoxInWindow1 = Application.Current.Windows.OfType<Window1>()?
    .FirstOrDefault()?.listBox1;

Window1.xaml:

<ListBox x:Name="listBox1" .... />
  • Related