Click here to Skip to main content
14,449,862 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear community,

in my WPF project I encountered a strange behavior: I want to update the content of a label by a DataTrigger if an Integer value reaches 0 (zero). Unfortunately that doesn´t work but for String values (= text, not numbers as text) it works. Even if I convert the Integer to String via .ToString() or CType() it doesn´t work. To recheck that I created a new WPF project but here I have the same problem. The Debugger doesn´t report an error.

My XAML of the new project:

<Window x:Class="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">
    <StackPanel>
        <Label Name="lbl">
            <Label.Style>
                <Style TargetType="Label">
                    <Setter Property="Content" Value="Ready" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=lbl, Path=Content}" Value="0">
                            <Setter Property="Content" Value="Done" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
        </Label>
        <Button Name="btn" Content="Start" Click="btn_Click_1" />
    </StackPanel>
</Window>

The code behind:

Imports System.Threading

Class MainWindow
    Private i As Integer = 0

    Private Async Sub btn_Click_1(sender As Object, e As RoutedEventArgs)
        i += 1

        Await Task.Run(Sub()
                           Thread.Sleep(2000)
                       End Sub)

        i -= 1
        lbl.Content = i
    End Sub
End Class

The cause seems to be the data type (Integer) even if the Value property uses the Object data type and should be able to work with Integers.

Because I do not know how to handle this I appreciate any help. Thanks!

What I have tried:

I tried to convert the Integer to String. Even if I create a separate String variable and take the value from there it doesn´t work:

Private i As Integer = 0
Private si As String = ""
...
i -= 1
si = i.ToString()
lbl.Content = si
Posted
Updated 7-Feb-20 1:45am
v2
Comments
Richard Deeming 7-Feb-20 6:35am
   
Await Task.Run(Sub()
    Thread.Sleep(2000)
End Sub)

Don't do that! You're spinning up a new thread just to have it sleep for two seconds.

Instead, use:
Await Task.Delay(2000)

It's less code, it's easier to read, and it's more efficient.
Michael____ 7-Feb-20 6:46am
   
thanks for the hint, the Sleep was only an example to see what happens ;-)
Richard Deeming 7-Feb-20 6:37am
   
Also, I'm not convinced by the idea of using a trigger on the Content property to alter the Content property every time the Content property changes.

You'd be better off using a binding with a value converter:
How to: Convert Bound Data - WPF | Microsoft Docs[^]
Gerry Schmitz 7-Feb-20 20:48pm
   
Content is either a string or a UIElement; a pure int is neither.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100