milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. C#

Display realtime log

Details
Written by: Stanko Milosev
Category: WPF
Published: 01 May 2014
Last Updated: 05 April 2022
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.

Open XAML without designer

Details
Written by: Stanko Milosev
Category: WPF
Published: 09 April 2014
Last Updated: 09 April 2014
Hits: 5725

Right click on the XAML file, and click "Open With..":

Choose Source Code (Text) Editor, and click "Set as Default":

Next time when you double click on XAML file, it will be opened without design, and faster.

Thanks to colleagues from ISE

Playing with triggers

Details
Written by: Stanko Milosev
Category: WPF
Published: 08 April 2014
Last Updated: 06 April 2022
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.

Dispatcher processing has been suspended, but messages are still being processed.

Details
Written by: Stanko Milosev
Category: WPF
Published: 02 April 2014
Last Updated: 30 November -0001
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.

  1. Update binding
  2. More about binding
  3. Autofac another example
  4. Resizable WPF controls

Subcategories

WPF

Beginning

Code snippets

NUnit

LINQ

Windows Forms

Page 27 of 39

  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31