数据绑定到文本框并在整个项目中共享对象 [英] Databinding to a textbox and sharing the object throughout the project

查看:31
本文介绍了数据绑定到文本框并在整个项目中共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新绑定文本框时遇到困难.我仍然是 WPF 开发的新手,我必须遗漏一个基本概念,因为此时我已经阅读了互联网上几乎所有可用的内容,但我仍然感到困惑.下面是我的代码.首先,概述我正在做什么以更好地为我的问题设置上下文.

I'm having difficulties with getting a bound textbox to update. I'm still new to WPF development and I must be missing a fundamental concept as I've read nearly everything available on the internet at this point and I'm still confused. Below is my code. First, an overview of what I'm doing to better set the context for my question.

Mainwindow 是一个包含使用框架源标签加载各种页面的选项卡的窗口.我相信这可能会导致我出现问题,因为我不确定每个选项卡的实际对象在哪里被实例化,只是 XAML 正在加载.

Mainwindow is a Window that contains tabs that load various pages using frame source tags. I believe this might be causing me issues as I'm not sure where the actual object is getting instantiated for each tab, just that the XAML is being loaded.

Scratchpad 是一个包含文本框的类,几乎所有执行任何类型操作以报告状态和任何错误的类都将更新和使用该文本框.

Scratchpad is a class that contains a textbox, which is going to be updated and used by almost all classes that perform any type of operation to report status and any errors.

Textbox XAML(这是在ScratchPad_View.xaml"中的上下文)

Textbox XAML (this is in "ScratchPad_View.xaml" for context)

<TextBox x:Name="scratchMessage" 
             Text="{Binding Path=ScratchMessage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             HorizontalAlignment="Right" 
             Height="300" 
             Width ="500" 
             TextWrapping="Wrap" 
             VerticalAlignment="Top"/>

XAML 背后的代码

public partial class ScratchPad : Page
{
    public ScratchPad()
    {
        InitializeComponent();
        ScratchPad_Model ScratchPad_Model = new ScratchPad_Model();
        this.DataContext = ScratchPad_Model;
    }
}

模型实现

class ScratchPad_Model : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public string _scratchMessage;
    public string ScratchMessage;
    {
        get
        {
            return _scratchMessage;
        }
        set
        {
            if (value != _scratchMessage)
            {
                _scratchMessage = value;
                OnPropertyChanged("ScratchMessage");
            }
        }
    }
    // Create the OnPropertyChanged method to raise the event 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我通过对 StackOverflow 上其他问题的回答和阅读大量数据绑定教程拼凑了其中的大部分内容,但它仍然没有点击.我不确定如何更新文本框的内容,并且由于我正在加载包含主窗口 XAML 中文本框的页面,因此我不确定我是否引用了正确的对象.主窗口在框架标记中加载此页面,复制如下.

Most of this I have cobbled together via responses to other questions on StackOverflow and reading numerous databinding tutorials however it's still not clicking. I'm not sure how to update the contents of the textbox and since I'm loading the page that contains the textbox in the XAML of my mainwindow I'm not sure I'm even referencing the correct object. The mainwindow loads this page in a frame tag, copied below.

<Frame Source="Common/View/ScratchPad_View.xaml" ></Frame>

在这个 XAML 背后的代码中,我有以下内容.

In the code behind for this XAML, I have the following.

public partial class MainWindow
{
    // Create scratchpad object for logging and status display
    ScratchPad scratchPad = new ScratchPad();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void StartVault(object sender, RoutedEventArgs e)
    {
        // Creates the authentication prompt view object and pass the scratchPad reference for reporting
        authPrompt_View _authPrompt_View = new authPrompt_View(scratchPad);
    }
}

我将在主窗口初始化时创建的对 ScratchPad 对象的引用传递给所有类,以便它们可以更新文本框的内容,但是我在使绑定工作方面运气不佳.一旦它工作,我仍然不太确定我应该如何将文本附加到文本框.这里可能有很多问题,但我希望解决我的一些概念问题,并更好地了解我做错了什么,提前致谢!

I pass the reference to the ScratchPad object that I created in the initialization of the mainwindow to all classes so that they can update the contents of the textbox, however I haven't had much luck in getting the binding to work. Once it works, I'm still not quite sure how I'm supposed to append text to the textbox. There's probably a great deal of problems here but I'm hoping to fix some of my conceptual problems and get a better understanding of what I'm doing wrong, thanks in advance!

推荐答案

您可以使用 Application.Properties 为您的项目设置全局属性.所以可能在文本框绑定变量的 SETTER 方法中(在您的情况下是 ScratchMessage),您需要在全局应用程序属性集合中设置属性.

You can use Application.Properties to set global properties for your project. So probably in SETTER method of textbox bound variable (in your case ScratchMessage), you need to set property in global application properties collection.

下面的链接解释得很好:https://msdn.microsoft.com/en-us/library/aa348545(v=vs.100).aspxhttp://www.c-sharpcorner.com/Resources/842/application-properties-in-wpf-and-c-sharp.aspx

Below links explains it very well: https://msdn.microsoft.com/en-us/library/aa348545(v=vs.100).aspx http://www.c-sharpcorner.com/Resources/842/application-properties-in-wpf-and-c-sharp.aspx

这篇关于数据绑定到文本框并在整个项目中共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆