1

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;
            }
        }
Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • Your property is `static` but you are binding to an _instance_ of `Application`. Don't mix & match; either just reference the static member directly (e.g. `{x:Static Application.runtime}`) or make the member an instance member (and then your binding should work). If that does not address your question, please provide [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) that reliably reproduces the problem. – Peter Duniho Oct 15 '15 at 16:10
  • @PeterDuniho, so if I reference it directly, it's like this right? `` – Martyn Ball Oct 15 '15 at 16:21
  • No. `Application.Current` would be `App.runtime` instead, if you wanted to use that syntax. – Peter Duniho Oct 15 '15 at 16:27
  • @PeterDuniho, that gives me the error `App is not supported in a Windows Presentation Foundation (WPF) Project)` – Martyn Ball Oct 15 '15 at 16:37
  • Yes, you probably need to include the namespace as well. E.g. something like `{x:Static l:App.runtime}` where you have an `xmlns` declaration in the XAML to define the `l` namespace. Again, without a good code example, it's impossible to say for sure what the exact syntax you need is. But had you searched on that error message, you'd have found helpful advice regarding that specific issue. – Peter Duniho Oct 15 '15 at 16:39
  • See for example http://stackoverflow.com/q/20683888, http://stackoverflow.com/q/5186805, http://stackoverflow.com/q/7622013, http://stackoverflow.com/q/32395, etc. – Peter Duniho Oct 15 '15 at 16:42
  • @PeterDuniho, thanks! I fixed my issue with the links you posted for me, however the way I did it now causes another problem, could you advise me on this please? – Martyn Ball Oct 15 '15 at 16:59
  • If you have a new problem, please post a new question. When you do, please be sure to provide [a good, **_minimal_**, **_complete_** code example](https://stackoverflow.com/help/mcve) that reliably reproduces the problem. – Peter Duniho Oct 15 '15 at 17:01

0 Answers0