简体   繁体   English

如何从其他窗口更新WPF列表框的内容

[英]How to update the content of wpf listbox from different window

I've two windows: Main Window, Log Window. 我有两个窗口:主窗口,日志窗口。 How can I update the listbox in the Log Window when some action is happened in the Main Window (eg button is clicked)? 当在主窗口中发生某些动作(例如,单击按钮)时,如何更新日志窗口中的列表框?

Below is the code for listbox in Log Window: 以下是“日志”窗口中列表框的代码:

<ListBox x:Name="DebugLogLb" BorderBrush="{x:Null}">  
  <TextBlock x:Name="DebugLogTb" Text="{Binding LogText}" Background="{x:Null}" />
</ListBox>

When the button in the Main Window is clicked, it will update the listbox. 单击主窗口中的按钮时,它将更新列表框。 I tried with the code below but it doesn't work. 我尝试使用下面的代码,但是它不起作用。

private void Btn1_Click(object sender, RoutedEventArgs e)
{
    var log = new LogWindow();
    log.DebugLogLb.Items.Add(new { LogText = "Button 1 is clicked" });
}

I'm able to update the listbox if I put everything in the same window, but I failed to do so with two windows. 如果将所有内容都放在同一个窗口中,则可以更新列表框,但是我无法在两个窗口中进行更新。

My expected output would be like: 我的预期输出将是: 在此处输入图片说明

Even if both windows are opened, when the buttons in the Main Window are clicked, it will directly update in the Log Window as well. 即使两个窗口都打开,单击主窗口中的按钮时,它也会直接在日志窗口中更新。

Thanks for any helps in advanced. 感谢您对高级的任何帮助。

It's hard to tell where you are going wrong without seeing more of the code. 在没有看到更多代码的情况下很难分辨出哪里出了问题。 This is an example that works. 这是一个有效的示例。 It creates a new LogWindow in the MainWindow ctor and sets the DataContext. 它在MainWindow ctor中创建一个新的LogWindow并设置DataContext。 When the button is clicked the handler calls show on the window. 单击该按钮后,处理程序调用将显示在窗口上。 The ListBox's itemssource property is bound to an ObservableCollection of strings. ListBox的itemssource属性绑定到字符串的ObservableCollection So any adds/removes are automatically updated on the UI. 因此,所有添加/删除操作都会在UI上自动更新。

LogWindows xaml LogWindows xaml

<Window x:Class="WpfApplication7.LogWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="LogWindow" Height="300" Width="300">
<Grid>
    <ListBox x:Name="DebugLogLb" BorderBrush="{x:Null}" ItemsSource="{Binding LogText}" />
</Grid>


MainWindow code-behind MainWindow的代码隐藏

public partial class MainWindow : Window
{
    LogWindow _logWindow;

    public MainWindow()
    {
        InitializeComponent();

        LogText = new ObservableCollection<string>();

        _logWindow = new LogWindow();
        _logWindow.DataContext = this;
        _logWindow.Closed += _logWindow_Closed;
    }

    private void _logWindow_Closed(object sender, EventArgs e)
    {
        _logWindow = new LogWindow();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        _logWindow.Show();

        LogText.Add("Button1 Clicked");
    }

    public ObservableCollection<string> LogText { get; set; }
}

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

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