简体   繁体   English

如何将字符串从辅助 window 传递到 WPF 中的主要 window

[英]How to pass a string from a secondary window to the main window in WPF

I am working on a to-do list application for a project.我正在为一个项目开发待办事项列表应用程序。 I would like to change the value of a string in an observableCollection.我想更改 observableCollection 中字符串的值。 I am able to change the string in the same window but I would like to change the value from a textbox in a secondary window.我可以更改同一个 window 中的字符串,但我想更改辅助 window 中文本框的值。

So what I tried to do was is change a string in the first window by using a textbox in the second window. By doing the way I have listed below it just blanks out the item I am trying to edit.因此,我尝试做的是通过使用第二个 window 中的文本框来更改第一个 window 中的字符串。按照我在下面列出的方法,它只会清空我要编辑的项目。

I would like to take the test from the textbox in the second window and use it to modify the taskName in the first window. Below I am going to include my code for the two c# files for the windows.我想从第二个 window 的文本框中进行测试,并用它来修改第一个 window 中的任务名称。下面我将包含我的代码,用于 windows 的两个 c# 文件。

This is the main window but it is called DemoMainWindow:这是主要的 window 但它被称为 DemoMainWindow:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ToDoList.ViewModels;
using ToDoList.Model;

namespace ToDoList
{
    /// <summary>
    /// Interaction logic for DemoMainWindow.xaml
    /// </summary>
    public partial class DemoMainWindow : Window
    {
        private ViewModel _viewModel;

        public string? EditedTaskName { get; set; }

        public DemoMainWindow()
        {
            InitializeComponent();

        
            TxtUCEnteredTask.txtLimitedInput.Text = "Do the dishes";

           _viewModel = new ViewModel();

            DataContext = _viewModel;


        }

        private void BtnAddTask_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.Tasks.Add(new TaskModel() { TaskName = TxtUCEnteredTask.txtLimitedInput.Text });
        }

        private void BtnDeleteTask_Click(object sender, RoutedEventArgs e)
        {
            if(LstBoxTasks.SelectedItem != null)
            {
#pragma warning disable CS8604 // Possible null reference argument.
                _ = _viewModel.Tasks.Remove(item: LstBoxTasks.SelectedItem as TaskModel);
#pragma warning restore CS8604 // Possible null reference argument.
            }
        }

        private void BtnHelp_Click(object sender, RoutedEventArgs e)
        {
            HelpWindow helpWindow = new HelpWindow();

            helpWindow.Show();
        }

        private string? GetEditedTaskName()
        {
            return EditedTaskName;
        }

        private void BtnEditTask_Click(object sender, RoutedEventArgs e)
        {
            if (LstBoxTasks.SelectedItem != null)
            {
                EditWindow editWindow = new EditWindow();
                editWindow.Show();
               //_viewModel.Tasks[LstBoxTasks.SelectedIndex].TaskName = editedTaskName;
            }
        }
    }
}

This is the code for the C# file of the second window:这是第二个window的C#文件的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ToDoList
{
    /// <summary>
    /// Interaction logic for EditWindow.xaml
    /// </summary>
    public partial class EditWindow : Window
    {
        public EditWindow()
        {
            InitializeComponent();
            var DemoMainWindow = this.DataContext;
        }

        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {
            ((DemoMainWindow)Application.Current.MainWindow).EditedTaskName = EditTextBox.Text;

            // _viewModel.Tasks[LstBoxTasks.SelectedIndex].TaskName = TxtUCEnteredTask.txtLimitedInput.Text;
        }
    }
}

When you create the view, create it's viewmodel before so that you can set the view's datacontext:创建视图时,先创建它的视图模型,以便您可以设置视图的数据上下文:

    EditWindowViewModel vm = new EditWindowViewModel();
    EditWindow editWindow = new EditWindow()
    {
        DataContext = vm;
    };
    editWindow.Show();
    StringToChange = vm.EditBoxTextProperty; 

Create a bindable property for stroing the editbox's text using INotifyPropertyCganged in the EditWindowViewModel ( see this ):使用 EditWindowViewModel 中的 INotifyPropertyCganged 创建一个可绑定属性以对编辑框的文本进行排序( 请参阅此):

    private string _editBoxTextProperty;
    public string EditBoxTextProperty
    {
        get => _editBoxTextProperty;
        set 
        {
            if (_editBoxTextProperty != value) 
            {
                _editBoxTextProperty = value;
                OnPropertyChanged();
            }
        }
    }

In the EditWindow xaml, use binding to connect the editbox's text value to your EditBoxTextProperty.在 EditWindow xaml 中,使用绑定将编辑框的文本值连接到您的 EditBoxTextProperty。

    <TextBox Text="{Binding Path=EditBoxTextProperty}"/>

Usefull links to build a proper WPF application: DataBinding MVVM Pattern构建合适的 WPF 应用程序的有用链接: DataBinding MVVM Pattern

What I have found to be useful when having to interact between elements on different windows is using x:FieldModifier当必须在不同 windows 上的元素之间进行交互时,我发现使用 x:FieldModifier 很有用

<TextBox x:Name="textbox" x:FieldModifier="public" />

You can then access the element through the instance of you window然后,您可以通过 window 的实例访问该元素

it will appear as WindowInstanceName.textbox它将显示为 WindowInstanceName.textbox

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

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