How can I modify a UIElement.property from another class? - c#

I'm working on a WinUI app (c# and xaml) with multiple frames and pages.
The problem is that I need to modify a UIElement property (TextBox.Text) from another class. I've been trying so many things and none of them has worked yet.
I'd be glad if someone could inspire me with some useful ways to do it. It can be anything aside from xaml data binding (<property={"Binding bindingName"}).
Thanks for the help.

In this sample code, I have 2 pages (Page1 and Page2) and 1 ViewModel (MainViewModel). Each page has a TextBox that are bound to the same property in the MainViewModel.
If you change the text in Page1, the text in Page2 will be changed, and vice versa.
NuGet packages
CommunityToolkit.Mvvm
Microsoft.Extensions.DependencyInjection
App.xaml.cs
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
namespace MultiplePagesSingleViewModel;
public partial class App : Application
{
private Window? window;
public App()
{
this.InitializeComponent();
Ioc.Default.ConfigureServices(
new ServiceCollection()
// This needs to be Singleton
.AddSingleton<MainViewModel>()
.BuildServiceProvider());
}
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
this.window = new MainWindow();
this.window.Activate();
}
}
Page1.xaml
<Page
x:Class="MultiplePagesSingleViewModel.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:MultiplePagesSingleViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<StackPanel>
<TextBlock Text="Page 1" />
<TextBox Text="{x:Bind ViewModel.SomeText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Page>
Page1.xaml.cs
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.UI.Xaml.Controls;
namespace MultiplePagesSingleViewModel;
public sealed partial class Page1 : Page
{
public Page1()
{
this.InitializeComponent();
// This MainViewModel is the same instance that Page2 gets.
ViewModel = Ioc.Default.GetRequiredService<MainViewModel>();
}
public MainViewModel ViewModel { get; }
}
Page2.xaml
<Page
x:Class="MultiplePagesSingleViewModel.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:MultiplePagesSingleViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<StackPanel>
<TextBlock Text="Page 2" />
<TextBox Text="{x:Bind ViewModel.SomeText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Page>
Page2.xaml.cs
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.UI.Xaml.Controls;
namespace MultiplePagesSingleViewModel;
public sealed partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
// This MainViewModel is the same instance that Page1 gets.
ViewModel = Ioc.Default.GetRequiredService<MainViewModel>();
}
public MainViewModel ViewModel { get; }
}
MainViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
namespace MultiplePagesSingleViewModel;
// This class needs to be "partial" for CommunityToolkit.Mvvm.
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
// The CommunityToolkit.Mvvm will automatically generate
// a UI-Interactive "SomeText" property for you.
private string someText = "Default text";
}
MainWindow.xaml
<Window
x:Class="MultiplePagesSingleViewModel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:MultiplePagesSingleViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid ColumnDefinitions="*,*">
<local:Page1 Grid.Column="0" />
<local:Page2 Grid.Column="1" />
</Grid>
</Window>

Well, finally after two weeks of research I've found and check a solution.
The solution is basically rising and catching events from the different classes. I found this information in a MSDN post. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines
This post is REALLY STRAIGHT FORWARD. I've created the CustomEventArgs class and then the publisher class is my controller (.cs) and my subscriber is the model (.xaml.cs).
The only problem that I've encountered is setting alll the methods and attributes as static in order to be able to call the controller function from other models. The problem is that on the raiseEvent(this, e); in the publisher class, this cannot be static so i've written null instead.
Also, I'd like to say that it works extremely fluent and with no lag or delay at all even if I constantly use it.
I hope this helps to everybody encountering the same problem.

Related

How can I write a method that returns the value of a of xaml toggle switch?

I have two xaml toggles in separate files that I want to update simultaneously (if one is switched on the other should be too (and vice versa). My first switch in xaml is:
<Switch Grid.Column="1" x:Name="toggleSwitch1" IsToggled="true" Toggled="OnToggled"/>
Using C# how can I return a boolean value of this switch so that I can update another switch simultaneously? Then once retrieving the value, how can I update the xaml of the toggle status for the other switch?
Your Switch control means, as I can understand, that you using UWP, but I'm not sure.
Anyway, the idea is to bind both controls IsToggled properties to same property of some ViewModel:
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace MyWPFApp
{
public class ControlsViewModel : INotifyPropertyChanged
{
private bool switchToggled;
public bool SwitchToggled
{
get => switchToggled;
set
{
switchToggled = value;
OnPropertyChanged(nameof(SwitchToggled));
}
}
public ControlsViewModel() { }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then in XAML of both Windows set bindings to Switch control (in my example - CheckBox control):
<!-- Window 1 -->
<Window x:Class="MyWPFApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPFApp"
mc:Ignorable="d"
Title="Window 1" Height="100" Width="300">
<Grid>
<CheckBox Content="Window1 CheckBox"
IsChecked="{Binding SwitchToggled}"/>
<!-- Replace IsChecked to IsToggled property -->
</Grid>
</Window>
<!-- Window 2 -->
<Window x:Class="MyWPFApp.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPFApp"
mc:Ignorable="d"
Title="Window 2" Height="100" Width="300">
<Grid>
<CheckBox Content="Window2 CheckBox"
IsChecked="{Binding SwitchToggled}"/>
<!-- Replace IsChecked to IsToggled property -->
</Grid>
</Window>
Code-behind of both Windows in example is same:
using System.Windows;
namespace MyWPFApp
{
public partial class Window1 : Window // or public partial class Window2
{
public Window1(ControlsViewModel cvm) // or public Window2
{
InitializeComponent();
DataContext = cvm;
}
}
}
And when calling that example Windows to show from Main one, you creating ControlsViewModel instance and pass it to both:
using System.Windows;
namespace MyWPFApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var cvm = new ControlsViewModel();
new Window1(cvm).Show();
new Window2(cvm).Show();
}
}
}
So checking/unchecking (toggle/untoggle) one of them will affect another and vice versa. Also, you can change SwitchToggled from code somewhere, which would affect both controls too.
Please note, that this is just example to try explain the idea. More MVVM pattern explanations and examples you can find at MSDN.

Viewmodel doesn't exist in namespace

I'm sorry if this is a stupid question or doesn't even fall under what I'm asking, but I'm new to WPF and I can't seem to get the hang of it. Right now I'm doing something akin to https://www.c-sharpcorner.com/article/use-inotifypropertychanged-interface-in-wpf-mvvm/ and I've run into a problem. When I try to execute my code:
namespace DPS_Calculator_Prototype
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow() {
InitializeComponent();
}
}
public class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChange(string propertyName) {
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
public class Calculator: NotifyPropertyChanged
{
private string _damage;
public string Damage {
get { return _damage; }
set {
_damage = value;
RaisePropertyChange("Damage");
}
}
}
namespace UseOf_INotifyPropertyChanged
{
public class MainWindowViewModel : Calculator
{
public MainWindowViewModel() {
Damage = "7";
}
}
}
}
I get the error that "The type or namespace name 'ViewModel' does not exist in the namespace 'DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged' (are you missing an assembly reference?)"
and
"The name "MainWindowViewModel" does not exist in the namespace 'clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel'."
and
"The type 'VM:MainWindowViewModel' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built."
I get the first error twice, once in MainWindow.g.cs and another in MainWindow.xaml. The other two are all in MainWindow.XAML If anyone can tell me what I'm doing wrong then that would be great. Here's the XAML file:
<Window x:Class="DPS_Calculator_Prototype.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel"
xmlns:local="clr-namespace:DPS_Calculator_Prototype"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<VM:MainWindowViewModel x:Name="VMMainWindow">
</VM:MainWindowViewModel>
</Window.DataContext>
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap"
Text="{Binding Damage}" VerticalAlignment="Top" Width="120"
Margin="78,28,0,0" TextChanged="TextBox_TextChanged"/>
</Grid>
</Window>
That's one of the worst "copy and paste" jobs I've ever seen guy...
I don't know where to start.
Just to run the application you MUST:
change the namespace VM as follows;
xmlns:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged"
DPSCalculatorPrototype.ViewModel doesn't exist.
The TextBox_TextChanged doesn't exist inside the codebehind of the window. You must add the method
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
//Do your stuffs
}
in the MainWindow class.
In order to avoid headaches to you or who is reading your code, you
SHOULD
use one .cs file for each class.
Avoid to nest namespaces inside the same .cs file and create a folder tree that replicates the namespace structure. In your snippet just create a UseOf_INotifyPropertyChanged folder inthe root and create the MainWindowViewModel class inside that.
The purpose of a namespace must be clear reading the code. Create a DPS_Calculator_Prototype.ViewModels and put all application viewmodel inside it.
I just tried using different namespaces and keep them more simple. And it works.
DPSCalculatorPrototype.ViewModel
namespace DPSCalculatorPrototype.ViewModel
{
public class MainWindowViewModel : Calculator
{
public MainWindowViewModel()
{
Damage = "7";
}
}
}
DPSCalculatorPrototype
namespace DPSCalculatorPrototype
{
public class Calculator : NotifyPropertyChanged
{
private string _damage;
public string Damage
{
get { return _damage; }
set
{
_damage = value;
RaisePropertyChange("Damage");
}
}
}
public class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChange(string propertyName)
{
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
MainWindow.xaml
<Window x:Class="DPSCalculatorPrototype.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:VM="clr-namespace:DPSCalculatorPrototype.ViewModel"
xmlns:local="clr-namespace:DPSCalculatorPrototype"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<VM:MainWindowViewModel x:Name="VMMainWindow"></VM:MainWindowViewModel>
</Window.DataContext>
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="{Binding Damage}" VerticalAlignment="Top" Width="120" Margin="78,28,0,0" TextChanged="TextBox_TextChanged" />
</Grid>
</Window>
MainWindow.xaml.cs
namespace DPSCalculatorPrototype
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
}
}
}
The reason you are seeing these errors is that WPF is looking in the namespaces mentioned and cannot seem to find what you're looking for. If you take a look at your XAML code you can see the line that says:
xmlns:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel"
This is what is declaring to use the namespace, so we need to point it to the correct area. Change your XAML to look like the following:
<Window x:Class="DPS_Calculator_Prototype.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged"
xmlns:local="clr-namespace:DPS_Calculator_Prototype"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<VM:MainWindowViewModel x:Name="VMMainWindow">
</VM:MainWindowViewModel>
</Window.DataContext>
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="
{Binding Damage}" VerticalAlignment="Top" Width="120" Margin="78,28,0,0"
TextChanged="TextBox_TextChanged"/>
</Grid>
</Window>
You were getting these errors because you had "ViewModel" in the namespace declaration, and this namespace doesn't exist, and as such, nothing exists in it.

User control wont create instance of datacontext in xaml

I have tried to figure out why a UserControl i have created cant have its DataContext set in xaml like
<UserControl.DataContext>
<vm:MainViewModel/>
</UserControl.DataContext>
If i do it throws an xamlparse exception saying DataContext=null.
if i instead in the codebehind do
this.DataContext = new MainViewModel();
there is no problem at all.
the MainView and MainViewModel are both dll projects.
The Main Window only contains MainView.
MainView and MainviewModel Code:
<UserControl x:Class="UmlEditor.view.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UmlEditor.view"
xmlns:vm="clr-namespace:UmlEditor.ViewModel;assembly=UmlEditor.ViewModel"
>
<!--This dosent work-->
<UserControl.DataContext>
<vm:MainViewModel/>
</UserControl.DataContext>
<Grid>
<Button Command="{Binding HeyCommand}" Content="i work" Width="100" Height="50"/>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.CommandWpf;
using System.Windows.Input;
namespace UmlEditor.ViewModel
{
public class MainViewModel
{
public ICommand HeyCommand { get; private set; }
public MainViewModel()
{
HeyCommand = new RelayCommand(hej);
}
private void hej()
{
Console.WriteLine("i am the bomb");
}
}
}
I Finally figured it out.
as both view and viewmodel are two seperate .dll and the app(window) a seperate project.
The app need reference to both .dll even though it only directly uses view

Binding data items on second window

Finally I decided to jump on the WPF bandwagon and decided to follow the MVVM Pattern to create my applications. I am also using Caliburn.Micro.
I found many examples of Binding data to Windows but all the examples contained just one MainWindow. I couldn't figure out how to reference and make the binding when opening a second and third window. To illustrate my problem I created a simple application. This application has two windows, the main one named ShellView and the second one named Window1View. All I need in this application is to display the content of myStr1 into the TextBox on Window1View.
Here is the code:
Views.ShellView.xaml
<Window x:Class="Test.Views.ShellView"
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="Title" />
<Button Content="Window 1" Height="31" HorizontalAlignment="Left" Margin="24,268,0,0" Name="btnWin1" VerticalAlignment="Top" Width="87" Click="btnWin1_click" />
</Grid>
</Window>
Views.ShellView.xaml.cs
namespace Test.Views
{
using System.Windows;
public partial class ShellView : Window
{
public ShellView()
{
InitializeComponent();
}
private void btnWin1_click(object sender, RoutedEventArgs e)
{
Window1View win1 = new Window1View();
win1.Show();
}
}
}
ViewModels.ShellViewModel.cs
namespace Test.ViewModels
{
using Caliburn.Micro;
public class ShellViewModel : PropertyChangedBase
{
public static string txt1 = "String 1";
public static string txt2 = "String 2";
private string title;
public string Title
{
get { return title; }
set
{
if (title != value)
{
title = value;
RaisePropertyChangedEventImmediately("Title");
}
}
}
public ShellViewModel()
{
Title = "Hello Caliburn.Micro";
}
}
}
Views.Window1View.xaml
<Window x:Class="Test.Views.Window1View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
Title="Window 1" Height="300" Width="300">
<Grid>
<Label Content="TextBox 1" Height="26" HorizontalAlignment="Left" Margin="12,40,0,0" Name="label1" VerticalAlignment="Top" Width="75" />
<TextBox Height="29" HorizontalAlignment="Left" Margin="106,39,0,0" Name="txtBox1" VerticalAlignment="Top" Width="145" Text="{Binding myStr1}" />
</Grid>
</Window>
View.Window1View.xaml.cs
using System.Windows;
namespace Test.Views
{
/// <summary>
/// Interaction logic for Window1View.xaml
/// </summary>
public partial class Window1View : Window
{
public Window1View()
{
InitializeComponent();
}
}
}
ViewModels.Window1ViewModel.cs
namespace Test.ViewModels
{
class Window1ViewModel
{
public Window1ViewModel()
{
myStr1 = ShellViewModel.txt1;
}
public string myStr1 { get; set; }
}
}
Bootstrapper.cs
namespace Test
{
public class Bootstrapper : Caliburn.Micro.Bootstrapper<Test.ViewModels.ShellViewModel>
{
}
}
App.xaml
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Test.App">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
namespace Test
{
using System.Windows;
public partial class App : Application
{
Bootstrapper bootstrapper;
public App()
{
bootstrapper = new Bootstrapper();
}
}
}
Any help with this would be greatly appreciated.
Thanks
Carmelo
Maybe I'm missing something because I'm not familiar with Caliburn.Micro, but I see a couple of things going on here.
First, I don't see anywhere that you're setting the DataContext of the Window1View to be an instance of Window1ViewModel.
Second, your Window1ViewModel doesn't derive from PropertyChangedBase and you don't call RaisePropertyChanged when you change the myStr1 property.
A common way to implement view model communication in MVVM is to use the Mediator Pattern. Most MVVM frameworks include a 'Messenger' class that allows you to decouple your design by publishing and subscribing to events. In Caliburn Micro, mediation is supported by the EventAggregator class.
Since you are new to MVVM, I would also recommend the following resources:
Implementing the MVVM Pattern
Advanced MVVM Scenarios
User Interaction Patterns
A few things here:
You are using code behind unnecessarily. Ideally you should aim for virtually no code behind when using MVVM. Instead of creating a btnWin1_click handler, name the button and implement a method on your view model with the same name. Caliburn.Micro will invoke the view model method based on convention.
Use x:Name rather than Name
When displaying the Window1ViewModel, you are not using Caliburn.Micro to invoke the window display. This means that no binding exists between your Window1View and Window1ViewModel. If this is a separate window, use the WindowManager type from Caliburn.Micro. Instantiate your Window1ViewModel, and use the WindowManager class to display it. Caliburn.Micro will locate the appropriate view based on conventions, and bind the view to your view model.
As mentioned, rather than reference the ShellViewModel directly in the Window1ViewModel (which couples the view models and makes Window1ViewModel less reuseable), use the mediator pattern. Caliburn.Micro comes with an EventAggregator class - you can publish the change in text from the ShellViewModel, and subscribe to the event in your Window1ViewModel.

WPF C# Classes, TextBox and Reference, Easy(?) "does not exist in the current context"

I'm pulling my hair out. I have created a class "employee.cs". I developed this class originally within the "public partial class Window1 : Window" on "Window1.xaml.cs". When moving it to a sepate class I can no longer refernce textBoxes, comboBoxes etc. What do I do?? Error given is "The name 'textBox1' does not exist in the current context". I'm sure its simple! Thanks Guys!
Here's a cut back example!
Window1.xaml
<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Height="100" Margin="12,12,23,0" Name="textBox1" VerticalAlignment="Top" />
</Grid>
</Window>
Window1.xaml.cs
namespace WpfApplication6
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
textBox1.Text = "testing"; //Works Here!
}
}
}
Class.cs
namespace WpfApplication6
{
class class1
{
public static void main()
{
textBox1.Text = "Help"; //Doesn't Work Here!! :-(
}
}
}
As the other answer here implies, you're going to need to change your class attribute in the Window XAML.
<Window x:Class="WpfApplication6.class1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Height="100" Margin="12,12,23,0"
Name="textBox1" VerticalAlignment="Top" />
</Grid>
</Window>
This change should make your textbox references work.
x:Class="WpfApplication6.Window1 in the xaml
Tells you this is part of the Window1 class. The window (from xaml) will be come a partial member of that class.

Categories

Resources