WPF绑定到应用程序属性
我需要绑定到App.xaml.cs类中的静态属性,到目前为止,我已经使用以下方法完成了此操作:WPF绑定到应用程序属性,wpf,binding,Wpf,Binding,我需要绑定到App.xaml.cs类中的静态属性,到目前为止,我已经使用以下方法完成了此操作: Property="{Binding Source={x.Static Application.Current}, Path=SomePath}" 当应用程序直接运行时,这可以正常工作,但是当应用程序从另一个应用程序启动时,这些绑定不会像我认为的应用程序那样工作。Current然后指向父应用程序,而不是xaml所在的应用程序 如何绑定到即时App.xaml.cs文件属性,而不是来自父应用程序的属性
Property="{Binding Source={x.Static Application.Current}, Path=SomePath}"
当应用程序直接运行时,这可以正常工作,但是当应用程序从另一个应用程序启动时,这些绑定不会像我认为的应用程序那样工作。Current然后指向父应用程序,而不是xaml所在的应用程序
如何绑定到即时App.xaml.cs文件属性,而不是来自父应用程序的属性
希望这有意义 到目前为止,我找到的一个解决方案是在App.xaml.cs和我试图绑定的xaml之间放置一个类: App.xaml.cs:
public partial class App : Application
{
public static string SomeText;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
SomeText = "Here is some text";
}
}
public class MyProperties
{
public static string SomeText
{
get { return App.SomeText; }
}
}
<Window.Resources>
<local:MyProperties x:Key="properties"/>
</Window.Resources>
<Grid>
<TextBlock Text="{Binding Source={StaticResource properties},
Path=SomeText}"/>
</Grid>
MyProperties.cs:
public partial class App : Application
{
public static string SomeText;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
SomeText = "Here is some text";
}
}
public class MyProperties
{
public static string SomeText
{
get { return App.SomeText; }
}
}
<Window.Resources>
<local:MyProperties x:Key="properties"/>
</Window.Resources>
<Grid>
<TextBlock Text="{Binding Source={StaticResource properties},
Path=SomeText}"/>
</Grid>
MainWindow.xaml:
public partial class App : Application
{
public static string SomeText;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
SomeText = "Here is some text";
}
}
public class MyProperties
{
public static string SomeText
{
get { return App.SomeText; }
}
}
<Window.Resources>
<local:MyProperties x:Key="properties"/>
</Window.Resources>
<Grid>
<TextBlock Text="{Binding Source={StaticResource properties},
Path=SomeText}"/>
</Grid>
任何其他建议仍然非常受欢迎:)什么是“从另一个应用程序开始”的意思?这是在一个不同的过程中吗?我不是特别清楚这一点!i、 e.Application1引用了Application2。然后在Application1下,类似于Application2.MainWindow app2Main=new Application2.MainWindow();在这种情况下,不会创建Application2的任何实例。。。