绑定到app.xaml.cs对象属性

时间:2015-10-15 15:56:54

标签: c# wpf xaml

我需要将文本字段绑定到对象实例中的属性,该实例位于app.xaml.cs.我怎样才能做到这一点?我只能找到引用单独定义的字符串的示例,我需要将所有字符串都包含在这一个对象中,以最小化代码背后的代码。

App.xaml.cs 这是我的类对象

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();
        }
    }

下面的代码是我创建对象实例的地方

<TextBox DataContext="{Binding Path=runtime, Source={x:Static Application.Current}}" Text="{Binding fromFolder}"></TextBox>

这是我正在尝试绑定数据的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>

修改

所以,我决定在我的应用程序资源中使用XAML实例化我的类,所以现在我可以从任何地方访问它。我现在的问题是,我的一些命令不再有效,因为他们需要引用类实例,如何从代码中的类引用中获取值?

的App.xaml

<TextBox Text="{Binding Source={StaticResource runtimeVariables},Path=fromFolder}" />

MainWindow.xaml

App.runtime

我的文本框现在将文本绑定到它。

Commands.cs public static void CloseWindow_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if (App.runtime.inProgress == true) { e.CanExecute = false; } else { e.CanExecute = true; } } 曾经是我的类实例,我将其更改为什么,所以它引用了我的类的XAML实例?

GoogleMaps.ready('eventsmap', function(map) {
   google.maps.event.addListener(map.instance, 'click', function(event) {
      Markers.insert({lat: event.latLng.lat(), lng: event.latLng.lng()});
   });
   ...

0 个答案:

没有答案
相关问题