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

C# 数据绑定,然后从WPF中的另一个类调用,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我的目的是在我的主UI窗口中添加一个文本块,如果需要,将更新其中的文本。为此,在我的UIXAML窗口中,我这样做: <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-names

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

<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>
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);
    }

    }

}
然后在我的主课中,我将其称为
UIWindow
,如下所示:

<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>
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”。这意味着
控制台文本
已成功更改。但是当我的UIWindow出现后,textblock是空的,我在那里看不到“Hello User!”。似乎
Text=“{Binding Path=consoleText}
part在我的xaml文件中工作不正常

其次也是最重要的一点,我想在另一个不同的类中更改
控制台文本
,即在
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.两种方法中的第一种在UI中设置值,只需在一行下方添加即可

在UIWindow类的构造函数中

this.DataContext=this;
//因为只指定属性consoletext,它将无法知道在哪里可以找到consoletext

2.你可以在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}"/

非常感谢您的回答。在您的帮助下,我解决了第一个问题和第二个问题的一半。因为在另一个类中更改consoleText后,我也可以看到消息框,但文本块没有更新,显示“Hello User”,但不显示新消息。我尝试添加“this.DataContext=this”“它的构造函数也是如此。你能指出吗?谢谢。在我的第二个问题中,我希望在我的文本块中看到“updatedConsoleText”。但在启动后显示“Hello User”,在我调用“changeConsoleOutput”函数时不更新文本。您是否检查了您的窗口级属性是否可以访问?我在这里看到了很多讨厌的代码;)但是,您也可以在xaml中直接设置datacontext。本地人:YourViewModel。使用galasoft mvvm light,您可以在viewmodel定位器中执行此操作,这很好,因为您可以轻松更改设计模式和常规模式datacontext@E_learner:太好了!在代码中绑定时,您需要实现INotifyPropertyChanged或使用dp,因此在更改代码中的属性/dp时,对这些属性的更改将发出重新绘制的信号。
myConsoleWindowTextBlock.Datacontext=consoleText;