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
- <Window x:Class="DispatcherTimer_CodeProject.test"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
xmlns:src="clr-namespace:DispatcherTimer_CodeProject"
-
x:Name="myDispatcherTimer"
-
Title="DispatcherTimer"
-
WindowStyle="None"
-
WindowState="Maximized"
-
ResizeMode="NoResize">
-
-
<Window.Resources>
-
<src:test x:Key="prevod"/>
-
<DataTemplate x:Key="grid1">
-
<Grid x:Name="myGrid" Background="Blue" Cursor="None">
-
<Grid.RowDefinitions>
-
<RowDefinition Height="*"/>
-
<RowDefinition Height="*"/>
-
</Grid.RowDefinitions>
-
<Grid.ColumnDefinitions>
-
<ColumnDefinition/>
-
</Grid.ColumnDefinitions>
-
<Label Grid.Row="0"
-
Grid.Column="0"
-
Name="DatumCas2"
-
Content="{Binding Source={StaticResource prevod}, Path=Promena, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
-
VerticalContentAlignment="Center"
-
HorizontalContentAlignment="Center">
-
</Label>
-
</Grid>
-
</DataTemplate>
-
<DataTemplate x:Key="grid2">
-
<Grid x:Name="myGrid"
-
Background="Black"
-
Cursor="None">
-
<Grid.RowDefinitions>
-
<RowDefinition Height="*"/>
-
<RowDefinition Height="*"/>
-
</Grid.RowDefinitions>
-
<Grid.ColumnDefinitions>
-
<ColumnDefinition/>
-
</Grid.ColumnDefinitions>
-
-
<Label Grid.Row="0"
-
Grid.Column="0"
-
Foreground="Red"
-
FontSize="50"
-
Content="CurrentDateAndTime"
-
VerticalContentAlignment="Center"
-
HorizontalContentAlignment="Center">
-
</Label>
-
<Label x:Name="DatumCas"
-
Grid.Row="1"
-
Grid.Column="0"
-
Foreground="White"
-
FontSize="30"
-
VerticalContentAlignment="Top"
-
HorizontalContentAlignment="Center">
-
</Label>
-
</Grid>
-
</DataTemplate>
-
</Window.Resources>
-
</Window>
test.xaml.cs
- using System;
-
using System.Windows;
-
using System.Windows.Controls;
-
using Microsoft.Win32;
-
using System.Windows.Input;
-
using System.Threading;
-
using System.ComponentModel;
-
using System.Windows.Threading;
-
-
namespace DispatcherTimer_CodeProject
-
{
-
public partial class test : Window, INotifyPropertyChanged
-
{
-
public event PropertyChangedEventHandler PropertyChanged;
-
-
private void NotifyPropertyChanged(string info)
-
{
-
if (PropertyChanged != null)
-
PropertyChanged(this, new PropertyChangedEventArgs(info));
-
}
-
-
private string promena;
-
-
public string Promena
-
{
-
get { return promena; }
-
set { promena = value; NotifyPropertyChanged("Promena"); }
-
}
-
-
public test()
-
{
-
-
InitializeComponent();
-
Promena = "Origin text";
-
this.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(test_MouseDoubleClick);
-
this.ContentTemplate = FindResource("grid1") as DataTemplate;
-
}
-
-
void test_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
-
{
-
myDispatcherTimer.Close();
-
}
-
-
private void UserControl_Loaded(object sender, RoutedEventArgs e)
-
{
-
System.Threading.Thread t = new Thread(new ThreadStart(Run));
-
t.SetApartmentState(ApartmentState.STA);
-
t.Start();
-
}
-
-
void Run()
-
{
-
Thread.Sleep(2000);
-
Promena = "Changed text";
-
}
-
}
-
}