Click here to Skip to main content
15,881,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Class

C#
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

XML
<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,

XML
<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 :

C#
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..
Posted
Updated 1-Jun-15 20:58pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900