数据绑定然后从WPF中的另一个类调用

时间:2014-07-23 07:45:22

标签: c# wpf xaml data-binding

我的目的是在我的主UI窗口中添加一个文本块,如果需要,将更新其中的文本。为此,在我的UIWindow xaml中,我确实喜欢这样:

<Window x:Class="UIDesigner.UIWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:UIDesigner"
    xmlns:c="clr-namespace:UIDesigner.Controls"
    WindowStartupLocation="CenterScreen"        
    WindowState="Maximized"
    WindowStyle="SingleBorderWindow"
    Title="GUI"
    Height="1000" Width="1400"
    HorizontalAlignment="Center"
    VerticalAlignment="Top"
    Icon="Resources/Images/Logo.png"        
    >

<Grid Margin="0">
    <Grid Grid.Row="1" Margin="0,10,0,0">

    <GroupBox Header="Console" Grid.Column="1" Margin="0,590,0,0" HorizontalAlignment="Stretch" x:Name="consoleWindow" IsEnabled="True" VerticalAlignment="Stretch"
                >
        <TextBlock x:Name="myConsoleWindowTextBlock" Text="{Binding Path=consoleText}"/>
    </GroupBox>
    </Grid>
</Grid>

</Window>

这是背后的代码:

using System.Windows;
using System.Runtime.CompilerServices;
using System.ComponentModel;

namespace UIDesigner
{
    public partial class UIWindow : Window
    {
        public UIWindow()
        {
            InitializeComponent();  
        }


        private string _consoleText;
        public string consoleText
        {
            get{ return _consoleText;}
            set
            {
                _consoleText = value;
                NotifyPropertyChanged("consoleText");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

然后在我的主要课程中,我这样称呼UIWindow

namespace UIDesigner
{
    public partial class Main : Window
    {
        public Main()
        {
            InitializeComponent();
        }

    private void LoginButton_Click_1(object sender, RoutedEventArgs e)
    {        
            var myUIWindow = new UIWindow();                
            myUIWindow.PropertyChanged += new PropertyChangedEventHandler(UIWindow_PropertyChanged);

            myUIWindow.consoleText = "Hello User!";
            myUIWindow.ShowDialog();

            this.Close();

    }

    private void LoginButton_MouseEnter_1(object sender, MouseEventArgs e)
    {   
    }

    static void UIWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        MessageBox.Show("Something Changed!");
        MessageBox.Show(e.PropertyName);
    }

    }

}

现在我有两个问题:

首先,当我的UI窗口启动时,我确实收到了两个消息框,说“改变了一些”,接着是“consoleText”。这意味着consoleText已成功更改。但是在我的UIWindow出现之后,文本块是空的,我看不到“Hello User!”那里。好像Text="{Binding Path=consoleText}部分在我的xaml文件中无法正常工作。

其次,最重要的是,我想更改另一个不同类中的consoleText,即DesignerCanvas.Commands.cs。为此我无法找出任何解决方案。我想在DesignerCanvas.Commands.cs

中找到类似这样的内容
namespace UIDesigner
{
    public partial class DesignerCanvas
    {

        private void changeConsoleOutput(string updatedConsoleText)
        {
            myUIWindow.consoleText = updatedConsoleText;  //obviously, this is not working
        }
    }
}

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:2)

1.首先在UI中设置值,只需添加一行

在UIWindow类的构造函数中

this.DataContext=this;

//因为只指定属性consoletext,它将无法知道在哪里找到consoletext。

2.u可以在App.Current.Windows中找到UIwindow并将其转换为UIWindow类型然后可以 进入酒店。

foreach(Window win in App.Current.Windows)
            {
                if (win as UIWindow != null)
                {
                    (win as UIWindow).consoletext = updatedConsoleText;
                }
            }

第二个问题

更改

<TextBlock x:Name="myConsoleWindowTextBlock" Text="{Binding Path=consoleText}"/

<TextBlock x:Name="myConsoleWindowTextBlock" Text="{Binding Path=.}"/

UIWindow构造函数集

中的

myConsoleWindowTextBlock.Datacontext=consoleText;