数据绑定到文本框并在整个项目中共享对象

时间:2017-05-17 23:00:07

标签: c# wpf xaml mvvm

我在获取更新的绑定文本框时遇到了困难。我还是WPF开发的新手,我必须错过一个基本概念,因为我已经阅读了互联网上几乎所有可用的东西,我仍然感到困惑。以下是我的代码。首先,概述我正在做些什么来更好地为我的问题设置上下文。

主窗口是一个窗口,其中包含使用帧源标记加载各种页面的选项卡。我相信这可能会引起我的问​​题,因为我不确定实际对象在哪里被实例化为每个选项卡,只是正在加载XAML。

Scratchpad是一个包含文本框的类,几乎所有执行任何类型操作的类都会更新并使用它来报告状态和任何错误。

Textbox XAML(在上下文的“ScratchPad_View.xaml”中)

<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中加载包含文本框的页面,我不确定我是否引用了正确的对象。主窗口将此页面加载到框架标记中,复制如下。

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

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

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对象的引用传递给所有类,以便它们可以更新文本框的内容,但是我没有太多运气来使绑定工作。一旦它工作,我仍然不太确定我应该如何将文本附加到文本框。这里可能存在很多问题,但我希望能够解决一些概念上的问题并更好地理解我做错了什么,提前谢谢!

2 个答案:

答案 0 :(得分:1)

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

以下链接解释得非常好: 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

答案 1 :(得分:1)

我的理解是,您已在构造函数中为ScratchPad创建了ViewModel,并在其中分配了DataContext。 因此,其他窗口将无法访问DataContext。

我的建议是维护一个基本ViewModel类并在所有其他ViewModel中继承基本Viewmodel。

在基本viewModel中添加ScratchMessage属性。 因此,您也可以从其他viewModel访问ScratchMessage属性。

 public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _scratchMessage;

        public string ScratchMessage
        {
            get { return _scratchMessage; }
            set
            {
                _scratchMessage = value;
                this.OnPropertyChanged("ScratchMessage");
            }
        }

        // Create the OnPropertyChanged method to raise the event 
        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

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

    public class ViewModel1 : BaseViewModel
    {
        ViewModel1()
        {
            this.ScratchMessage = "Message";
        }
    }