问题
I have a static class in a common project where I have all my property with all my text (for example, all my titles)
I want to know how I can bind my TextBlock Text to this value.
I tried Text={x:Static...} but Static is not found.
Thanks
回答1:
{x:static...} is not present in UWP.
You can still do something similar, but the class itself must not be static. The properties in the class can be static but you need to create an instance of that class. So you will need to ask for a change in the core lib.
Then you declare that class as a resource and use that as the source of your Bindings. I recommend you declare the resource where it's globally available, like app.xaml.
<Application.Resources>
<lib:Class1 x:Key="c1"/>
</Application.Resources>
...
<TextBlock Text="{Binding Source={StaticResource c1}, Path=Text1}" />
回答2:
You may set Converterwith your static value return:
<TextBlock Text="{Binding Converter={StaticResource GetStatiField}}"/>
Now according to your DataContext you should write your return logic:
public sealed class GetStatiField: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
//value is a type of your TextBlock.DataContext
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
来源:https://stackoverflow.com/questions/39978461/uwp-and-binding-to-static-class-in-different-project