I have a Class
public class Emp
{
public string EmpName { get; set; }
}
and I am using this class as a Resource in Main Window of WPF by importing its namespace as local
<Window.Resources>
<local:Emp x:Key="Mydatasource" EmpName="Siraj" ></local:Emp>
</Window.Resources>
I have a TextBlock and a Button in my window.
TextBlock is bind to the resource's EmpName property which I have defined in resource,
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="129,56,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBlock Name="txtblock1" HorizontalAlignment="Left" Margin="65,59,0,0" TextWrapping="Wrap" Text="{Binding Source={StaticResource Mydatasource}, Path=EmpName}" VerticalAlignment="Top" RenderTransformOrigin="0.994,-1.257"/>
</Grid>
and as I click on the Button, I want to change the value of the Resource's EmpName property so, I wrote the code as :
private void Button_Click(object sender, RoutedEventArgs e)
{
Emp emp = new Emp();
emp.EmpName = "Hussaini";
this.Resources["Mydatasource"] = emp;
}
But while running the app, as we click on the Button, the value of the resource is getting changed, but it is not reflecting the TextBlock which is bind to that resource.
Please find a solution give the answer by Testing it..