By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,319 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,319 IT Pros & Developers. It's quick & easy.

WPF Canīt change content of label by databinding

P: 2
Hello, I have problem with databinding. I created small application using structure that I need to demonstrate problem.
I need to change content of label when changing content of property "Promena". I change content of property "Promena" from "Origin text" to "Changed text" using thread but content of label is still the same - databinding isnīt working according to my vision.
Does somebody know, please, why content of label doesnīt change when I change content of property "Promena"?

test.xaml
Expand|Select|Wrap|Line Numbers
  1. <Window x:Class="DispatcherTimer_CodeProject.test" 
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.         xmlns:src="clr-namespace:DispatcherTimer_CodeProject" 
  5.         x:Name="myDispatcherTimer" 
  6.         Title="DispatcherTimer" 
  7.         WindowStyle="None" 
  8.         WindowState="Maximized" 
  9.         ResizeMode="NoResize">  
  10.  
  11.     <Window.Resources>    
  12.         <src:test x:Key="prevod"/>  
  13.         <DataTemplate x:Key="grid1">  
  14.             <Grid x:Name="myGrid" Background="Blue" Cursor="None">  
  15.                 <Grid.RowDefinitions> 
  16.                     <RowDefinition Height="*"/>  
  17.                     <RowDefinition Height="*"/>  
  18.                 </Grid.RowDefinitions> 
  19.                 <Grid.ColumnDefinitions> 
  20.                     <ColumnDefinition/> 
  21.                 </Grid.ColumnDefinitions> 
  22.                 <Label Grid.Row="0"    
  23.                Grid.Column="0"    
  24.                        Name="DatumCas2" 
  25.                Content="{Binding Source={StaticResource prevod}, Path=Promena, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
  26.                VerticalContentAlignment="Center"   
  27.                HorizontalContentAlignment="Center">  
  28.                 </Label> 
  29.             </Grid> 
  30.         </DataTemplate> 
  31.         <DataTemplate x:Key="grid2">  
  32.             <Grid x:Name="myGrid" 
  33.           Background="Black" 
  34.           Cursor="None">  
  35.                 <Grid.RowDefinitions> 
  36.                     <RowDefinition Height="*"/>  
  37.                     <RowDefinition Height="*"/>  
  38.                 </Grid.RowDefinitions> 
  39.                 <Grid.ColumnDefinitions> 
  40.                     <ColumnDefinition/> 
  41.                 </Grid.ColumnDefinitions> 
  42.  
  43.                 <Label Grid.Row="0"   
  44.                Grid.Column="0"   
  45.                Foreground="Red" 
  46.                FontSize="50" 
  47.                Content="CurrentDateAndTime" 
  48.                VerticalContentAlignment="Center" 
  49.                HorizontalContentAlignment="Center">  
  50.                 </Label> 
  51.                 <Label x:Name="DatumCas" 
  52.                Grid.Row="1" 
  53.                Grid.Column="0" 
  54.                Foreground="White" 
  55.                FontSize="30" 
  56.                VerticalContentAlignment="Top" 
  57.                HorizontalContentAlignment="Center">  
  58.                 </Label> 
  59.             </Grid> 
  60.         </DataTemplate> 
  61.     </Window.Resources> 
  62. </Window> 

test.xaml.cs
Expand|Select|Wrap|Line Numbers
  1. using System;  
  2. using System.Windows;  
  3. using System.Windows.Controls;  
  4. using Microsoft.Win32;  
  5. using System.Windows.Input;  
  6. using System.Threading;  
  7. using System.ComponentModel;  
  8. using System.Windows.Threading;  
  9.  
  10. namespace DispatcherTimer_CodeProject  
  11. {  
  12.     public partial class test : Window, INotifyPropertyChanged  
  13.     {     
  14.         public event PropertyChangedEventHandler PropertyChanged;  
  15.  
  16.         private void NotifyPropertyChanged(string info)  
  17.         {  
  18.             if (PropertyChanged != null)  
  19.                 PropertyChanged(this, new PropertyChangedEventArgs(info));  
  20.         }  
  21.  
  22.         private string promena;  
  23.  
  24.         public string Promena  
  25.         {  
  26.             get { return promena; }  
  27.             set { promena = value; NotifyPropertyChanged("Promena"); }  
  28.         }  
  29.  
  30.         public test()  
  31.         {  
  32.  
  33.             InitializeComponent();  
  34.             Promena = "Origin text";  
  35.             this.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(test_MouseDoubleClick);  
  36.             this.ContentTemplate = FindResource("grid1") as DataTemplate;  
  37.         }  
  38.  
  39.         void test_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)  
  40.         {  
  41.            myDispatcherTimer.Close();  
  42.         }  
  43.  
  44.         private void UserControl_Loaded(object sender, RoutedEventArgs e)  
  45.         {  
  46.             System.Threading.Thread t = new Thread(new ThreadStart(Run));  
  47.             t.SetApartmentState(ApartmentState.STA);  
  48.             t.Start();  
  49.         }  
  50.  
  51.         void Run()  
  52.         {  
  53.             Thread.Sleep(2000);  
  54.             Promena  = "Changed text";  
  55.         }  
  56.     }      
Nov 15 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.