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