Reputation: 163
I have:
<Page.Resources>
<data:PublishManager x:Key="pubManager"/>
</Page.Resources>
then in my textBlock i used this:
<TextBlock Grid.Row="2" Canvas.ZIndex="3" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Path=SocialStatus, Mode=TwoWay, Source={StaticResource pubManager}}"></TextBlock>
my class PublishManager look like this:
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string _SocialStatus;
public string SocialStatus
{
get
{
return _SocialStatus;
}
set
{
_SocialStatus = value;
RaisePropertyChanged("SocialStatus");
}
}
why when i write in my method code something like this it's don't work for me?
SocialStatus = "StackOverflow";
Why my page with TextBlock don't refresh content?
Upvotes: 1
Views: 267
Reputation: 8823
Do not use the StaticResource here. They are used where resource value is not likely to change. Read below links:
if the value of SocialStatus is always going to be StackOverflow then declare the static property with that value and your binding should work. else you have to create an object of the class and give it as a datacontext to the view.
Or just create a datacontext when required
<TextBlock Grid.Row="2" Canvas.ZIndex="3" HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{Binding Path=SocialStatus, Mode=TwoWay}">
<TextBlock.DataContext>
<data:PublishManager/>
</TextBlock.DataContext>
</TextBlock>
this will work too.
Upvotes: 2
Reputation: 39
https://msdn.microsoft.com/en-us/library/cc838207%28v=vs.95%29.aspx
binding to a static source works. You just have to make sure data: maps to right namespace. As you did not provide the complete Xaml. you might want to check this.
<UserControl x:Class="PublishManager.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:namespace-to-code">
Although best practice is to use the DataContext to make it more reusable. Like the answer of Patryk provided.
Upvotes: 1
Reputation: 486
The problem is that you are using static resource in your binding scenario. Static resources aren't monitored in case of property changes. Do you really need to use your PublishManager
as page resource?
It would be better when an instance of PublishManager
will be set as DataContext
.
So firstly set Page.DataContext
:
<Page.DataContext>
<data:PublishManager/>
</Page.DataContext>
And later bind to context property:
<TextBlock Grid.Row="2" Canvas.ZIndex="3" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding SocialStatus}"/>
Upvotes: 3