Reputation: 2741
I have a text block in which Text is binded from resource file,all works fine but GetBindingExpression always return null. Is there any other way of binding
<TextBlock x:Name="Slide"
Text="{x:Static prop:Resources.SlideToCollect}"/>
Slide.GetBindingExpression(TextBlock.TextProperty)
Upvotes: 0
Views: 267
Reputation: 125
x:Static - it is NOT binding, and it not creates any BindingExpression instances. x:Static - it is a WPF markup extension, that allows you reference to any static by-value code entity that is defined in a CLS–compliant way. In your case - you get value from Resource.SlideToCollect static property(or field, or constant, etc.) of Resource class and assign it to Text property of your TextBlock.
If you want use exactly Binding, so you need Binding markup extension. Here example code:
<Window
x:Class="WpfApplication66.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:WpfApplication66"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources>
<app:Resources x:Key="Resources"/>
</Window.Resources>
<Grid>
<TextBlock x:Name="Slide" Text="{Binding SlideToCollect, Source={StaticResource Resources}}"/>
</Grid>
And code behind for window:
public class Resources
{
public string SlideToCollect { get { return "i'am SlideToCollect"; } }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
BindingExpression be = Slide.GetBindingExpression(TextBlock.TextProperty);
}
}
Now, variable "be" will assigned with instance of BindingExpression class. Don't forget replace WpfApplication66 with your project name ;)
Upvotes: 1