I am a NOVICE and am very much struggling with what seems should be a very simple task. How do I modify a property of a MainWindow
TextBlock
, from another cs file. An exact code solution would be extremely helpful.
Below is the stripped down code. Is my use of static class causing me extra issues?
In 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>
In File: MainWindow.xaml.cs
namespace TestApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); TextBlock1.Text = "Setting Text from MainWindow"; MyProgram.myProgramStart(); } } }
In 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"; } } }
Access of the controls from within the same assembly is hence allowed. If you want to access a control on a wpf form from another assembly you have to use the modifier attribute x:FieldModifier="public" or use the method proposed by Jean.
FindName method of FrameworkElement class is used to find elements or controls by their Name properties.
Controls in WPF are accessed by their names (and the Name property). We specify the Name property in the XAML, and then can access the control by that name directly in C# code. This allows controls to interact. Example.
If you look at App. xaml class of your WPF application, you will see the following XAML code. Here the StartupUri sets the startup Window of an application. If you want to change the Startup window to some other window, just change this value.
Because nobody else has actually answered the question I'm going to tell you how to achieve what you want, but do listen to the posters who said that in a real application you would use MVVM. However there are times when you need to do what you ask so the code you need is:
((MainWindow)System.Windows.Application.Current.MainWindow).TextBlock1.Text = "Setting Text from My Program";
You can simply achieve this using MVVM. You shouldn't directly access controls in View using its name from another class. You have to use binding properties.
First of all, add a class. This will be your ViewModel. Add your properties to this class which will be binded to your input controls in your View.
Student ViewModel
public class Student { public string Name { get { return "Setting Text from My Program"; } } }
App.Config
Now you have add to this View Model as a resource in your App.Config file. First, add the name space reference to your app.config where your VM resides. [xmlns:local="clr-namespace:WpfApplication2]. Add your VM class as a resource by specifying your View Model class name (student).
<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
Set the DataContext
with the resource key added to App.config and set the binding to the property defined in the Student View Model.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With