我是一个NOVICE,我正在努力应对看似非常简单的任务。如何从另一个cs文件修改MainWindow
TextBlock
的属性。精确的代码解决方案非常有用。
以下是精简代码。我使用静态类会给我带来额外的问题吗?
在File:MainWindow.xaml
中<Window x:Class="TestApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock x:Name="TextBlock1" HorizontalAlignment="Left" Margin="107,71,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
</Grid>
</Window>
在File:MainWindow.xaml.cs
中namespace TestApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TextBlock1.Text = "Setting Text from MainWindow";
MyProgram.myProgramStart();
}
}
}
在File:CodeFile1.cs
中namespace TestApp1
{
public static class MyProgram
{
public static void myProgramStart()
{
// ... blah blah blah
// I want to do something like follows, but won't compile
MainWindow.TextBlock1.Text = "Setting Text from My Program";
}
}
}
答案 0 :(得分:40)
因为没有其他人真正回答过这个问题我会告诉你如何实现你想要的东西,但是请听那些在实际应用程序中说你会使用MVVM的海报。但有时你需要做你想要的,所以你需要的代码是:
((MainWindow)System.Windows.Application.Current.MainWindow).TextBlock1.Text = "Setting Text from My Program";
答案 1 :(得分:15)
您可以使用MVVM简单地实现此目的。您不应该使用其他类中的名称直接访问View中的控件。您必须使用绑定属性。
首先添加一个类,这将是您的 ViewModel 将您的属性添加到此类,该类将绑定到查看中的输入控件。
学生ViewModel
public class Student
{
public string Name
{
get { return "Setting Text from My Program"; }
}
}
<强> App.Config中强>
现在,您已将此View Model添加为 App.Config 文件中的资源。首先将名称空间引用添加到VM所在的app.config中。 [xmlns:local =“clr-namespace:WpfApplication2]。通过指定View Model类名(student)将您的VM类添加为资源。
<Application x:Class="WpfApplication2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
xmlns:local="clr-namespace:WpfApplication2">
<Application.Resources>
<local:Student x:Key="Student" />
</Application.Resources>
</Application>
<强> MainWindow.xaml 强>
将DataContext
设置为添加到App.config的资源键,并将绑定设置为学生视图模型中定义的属性。
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{StaticResource Student}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Text="{Binding Name}" Height="23" HorizontalAlignment="Left" Margin="127,124,0,0" Name="textBlock1" VerticalAlignment="Top" Width="214" />
</Grid>
</Window>
答案 2 :(得分:3)
基本上有2-3种以上的方法。给定的方法更容易理解和处理。 您可以通过以下代码(1),(2),(3),(4)访问MainWindow控件。
在File:MainWindow.xaml.cs
中 public partial class MainWindow
{
internal static MainWindow Main; //(1) Declare object as static
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Main =this; //(2) Defined Main (IMP)
var AnyClassORWindow=new Class1(); //Initialize another Class
AnyClassORWindow.ShowValue();
}
}
在File:Class1.cs
中 internal class Class1 : MainWindow //(3) Inherited
{
internal void Display()
{
MessageBox.Show(Main.TextBox1.Text); //(4) Access MainWindow Controls by adding 'Main' before it.
}
}
备注: - 强>
- 在LOADED窗口之后不在CONSTRUCTOR中使用代码(2)的好习惯。
- 构造函数中的代码(2)可能会留下运行时问题。
- 另一个简单的方法是使用&#39; ref MainWindow_field&#39;通过传递到每个类的构造函数或指定&#39;(MainWindow)Application.Current.MainWindow&#39; to static Main。
醇>
答案 3 :(得分:1)
使用MVVM pattern
访问控件的属性并进行修改:
public class Student
{
public Student()
{
}
public string Name
{
get { return "Setting Text from My Program"; }
}
}
在后面的代码中设置DataContext
的{{1}}:
XAML
将Text属性绑定到Name:
this.DataContext = new Student();
答案 4 :(得分:0)
您需要创建MainWindow
。
但是没有理由这样做,因为它将在WPF应用程序中自动完成。除非你有特定的理由这样做(我怀疑是因为这个问题,因为你说你是新手)。
答案 5 :(得分:0)
至于为什么它不会编译,我会假设你得到的编译器错误是......
An object reference is required for the non-static field, method, or property 'TestApp1.MainWindow.TextBlock1'
这是因为您尝试访问TextBlock1
,就好像它是静态的一样。正如@JeffRSon所述,首先创建一个MainWindow类的实例。
// Create an instance of MainWindow
var mainWindow = new MainWindow();
mainWindow.TextBlock1.Text = "Setting Text from My Program";
我想你可能也想要显示窗口......
mainWindow.ShowDialog();
答案 6 :(得分:0)
为了继续弥敦道所说的,我使用了一个安全的演员表:
(System.Windows.Application.Current.MainWindow as MainWindow)?.TextBlock1.Text = "Setting Text from My Program";
请注意answer Nathan gave上的注释。这不是理想的方法,但是可行。