I need to bind a text field to a property in an object instance, which is in app.xaml.cs. How can I do that? I can only find examples of referencing individually defined strings, I need my strings to all be contained in this one object to minimize the code behind code.
App.xaml.cs Here is my class object
/// <summary>
/// Global values for use during application runtime
/// </summary>
public class runtimeObject
{
//Can the application be closed?
private bool _inProgress = false;
public bool inProgress
{
get { return _inProgress; }
set { _inProgress = value; }
}
//Selected folder to search in
private string _fromFolder = "testing string";
public string fromFolder
{
get { return _fromFolder; }
set { _fromFolder = value; }
}
}
The code below is where I create the instance of the object
public partial class App : Application
{
public static runtimeObject runtime { get; set; }
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//Startup
Window main = new MainWindow();
main.Show();
//Bind Commands
Classes.MyCommands.BindCommandsToWindow(main);
// Create runtime objects
// Assign to accessible property.
runtime = new runtimeObject();
}
}
Here is my XAML where i'm attempting to bind to the data.
<TextBox DataContext="{Binding Path=runtime, Source={x:Static Application.Current}}" Text="{Binding fromFolder}"></TextBox>
Edit
So, I decided to instantiate my class using XAML within my Application Resources, so now I have access to it from anywhere. My issue now, is that some of my commands no longer work, as they need to have reference of the class instance, how can I get the values from my class reference in my code behind?
App.xaml
<Application x:Class="Duplicate_Deleter.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Duplicate_Deleter">
<Application.Resources>
<local:runtimeObject x:Key="runtimeVariables" />
</Application.Resources>
</Application>
MainWindow.xaml
<TextBox Text="{Binding Source={StaticResource runtimeVariables},Path=fromFolder}" />
My textbox now has my text binded to it.
Commands.cs
App.runtime
used to be my class instance, what do I change this to, so it references my XAML instance of the class?
public static void CloseWindow_CanExecute(object sender,
CanExecuteRoutedEventArgs e)
{
if (App.runtime.inProgress == true)
{
e.CanExecute = false;
}
else
{
e.CanExecute = true;
}
}