I have a very strange issue with an WPF (.NET 7.0) project.
I use databinding to display elements of an (ordered) list in a WrapPanel:
foreach (DatenElement element in BruteForce_Ordered.Do(randomData.ElementAtOrDefault(0), randomData))
{
ElementDisplay display = new ElementDisplay();
display.DataContext = element;
Debug.WriteLine(element.Distance.ToString() + " | " + element.IDShortDisplay);
Wrap_Panel.Children.Add(display);
}
The output window of Visual Studio shows a correctly ordered list of elements:
However, the actual Display of the ElementDisplay items show for two (marked red) of the Elements wrong values:
The ID-values (9ecbde and 728145) are the expected ones, but the values displayed are wrong. Here is the Code for the ElementDisplay:
<StackPanel Orientation="Horizontal">
<TextBlock Text="ID:" Margin="0, 0, 2, 0" />
<TextBlock x:Name="TextBlock_ID" Text="{Binding IDShortDisplay, FallbackValue=XYZ}" FontWeight="DemiBold" Margin="0, 0, 2, 0" ToolTip="{Binding ID}"/>
<Border Background="LightGray" Padding="1, 0, 1, 0">
<TextBlock Text="{Binding ValueDisplay, FallbackValue=0.000}" />
</Border>
</StackPanel>
End here is the code for my DatenElement class:
internal class DatenElement
{
public string ID { get; set; }
public double[] Values { get; private set; }
public int Index { get; private set; }
public double? Distance { get; set; }
public string ValueDisplay => Distance?.ToString("##0.000") ?? "-";
public string IDShortDisplay => ID.SaveSubString(0, 6);
...
}
SaveSubString is an Extension, the uses SubString, but checks parameters first.
The behavior with some wrongly displayed values is reproducable, but always at different element positions. Any idea, what the cause might be?
1