绑定Xaml WPF中的静态属性
问题描述:
亲爱的所有人,
我有一个类(Session),它包含静态属性。静态属性包含Brush类型属性(ForeGround)。我想在wpf xaml中的资源中绑定Foreground属性。
(Foreground ={Binding Source = {StaticResource session},Path = Object.ForeGround} )不起作用
我无法绑定Foreground属性。请帮助
以下是示例代码
Dear All,
I have a class (Session), which contain static property. Static property contain Brush type property (ForeGround). I want to bind Foreground property in a resource in wpf xaml.
(Foreground="{Binding Source={StaticResource session}, Path=Object.ForeGround}") does not work
I am unable to bind Foreground property. Please Help
Below is sample code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Session.Object = new Info() { ForeGround = Brushes.White };
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
//If I quick watch item. i got session object, and required foreground value is Ok.
var item = this.FindResource("session");
}
}
//*****************************************
public class Session
{
public static Info Object
{
get;
set;
}
}
//**************************************
public class Info
{
public Brush ForeGround
{
get;
set;
}
}
这里是WPF XAML
and here is WPF XAML
<Window.Resources>
<local:Session x:Key="session"/>
</Window.Resources>
<Grid removed="Gray">
<Viewbox>
<TextBlock Name="txt" Text="Hello Text" Foreground="{Binding Source={StaticResource session}, Path=Object.ForeGround}"/>
</Viewbox>
</Grid>
答
看看: http://*.com/questions/936304/binding-to-static-property [ ^ ]
我认为这是单向绑定吗?要绑定到静态,可以使用x:静态绑定表达式。
take a look at : http://*.com/questions/936304/binding-to-static-property[^]
I take it this is one way binding? To bind to statics you can use the x:Static binding expression.
这可能适用于您要实现的目标:
http://*.com/questions/936304/binding-to-static-property [ ^ ]
如果没有,就有这篇文章:
静态属性的动态绑定 [ ^ ]
This might work for what you are trying to achieve:
http://*.com/questions/936304/binding-to-static-property[^]
If not, there is this article:
Dynamic Binding of Static Properties[^]