WPF Binding To Application Properties
I need to bind to static properties in my App.xaml.cs class and have so far done this using:
Property="{Binding Source={x.Static Application.Current}, Path=SomePath}"
This works OK when the application is being run directly, but when the application is started from another application, these bindings don't work as I believe Application.Current then points at the parent application rather than the application that 开发者_运维问答the xaml sits under.
How would I bind to the immediate App.xaml.cs file properties rather than those from the parent application?
Hope that makes sense!
So one solution I've found so far is to put a class between App.xaml.cs and the XAML I'm trying to bind:
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";
}
}
MyProperties.cs:
public class MyProperties
{
public static string SomeText
{
get { return App.SomeText; }
}
}
MainWindow.xaml:
<Window.Resources>
<local:MyProperties x:Key="properties"/>
</Window.Resources>
<Grid>
<TextBlock Text="{Binding Source={StaticResource properties},
Path=SomeText}"/>
</Grid>
Any other suggestions are still more than welcome :)
App.xaml.cs:
public partial class App : Application
{
public static string SomeText => "Here is some text";
}
MainWindow.xaml:
<Grid>
<TextBlock Text="{Binding Source={x:Static Application.Current},
Path=SomeText}"/>
</Grid>
精彩评论