- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 4854
If you want, for example, to show in a text box loop iterations in a real time, then you have to use something like:
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(
() =>
{
DisplayCount = i.ToString();
OnPropertyChanged(() => this.DisplayCount);
System.Threading.Thread.Sleep(10);
}));
Where DiplayCount is property to which TextBox is binded. Here I explained how to use INotifyPropertyChanged, and from here you can download example.
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 5294
First create dictionary as I explained here.
In my case I've created TriggersDictionary.xaml, so my main window XAML looks like this:
<Window x:Class="PlayingWithResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source= "TriggersDictionary.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Rectangle Style="{StaticResource styleWithTrigger}"></Rectangle>
</Grid>
</Window>
My style (TriggersDictionary.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="styleWithTrigger" TargetType="Rectangle">
<Setter Property="Fill" Value="LightGreen" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
Here you can see property - this is event which will be executed as well as target type (in my case rectangle)
Example project you can download from here, and this article I wrote using this page.
----
2014-06-16 Update Properties which are supported for trigger, for rectangle, for example you can see here.
- Details
- Written by: Stanko Milosev
- Category: WPF
- Hits: 11371
I was playing with TabControl, and I tried to implement SelectionChange event of TabControl.
So I did something like this:
XAML:
<Window x:Class="WpfApplication20.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="Test1"></TabItem>
<TabItem Header="Test2"></TabItem>
</TabControl>
</Grid>
</Window>
Code behind:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("test");
}
With that code I was receiving error:
Dispatcher processing has been suspended, but messages are still being processed.
Solution:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(this.MyTest));
}
private void MyTest()
{
MessageBox.Show("test");
}
It seems that problem is in multi thread... Because for example code like this:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// ... Get TabControl reference.
var item = sender as TabControl;
// ... Set Title to selected tab header.
var selected = item.SelectedItem as TabItem;
this.Title = selected.Header.ToString();
}
Works without problem.

