Fixed some minor ui things with constraints

This commit is contained in:
Frederik Jacobsen 2025-10-18 20:01:39 +02:00
parent 14bad15f4a
commit eacfcf611c
2 changed files with 40 additions and 2 deletions

View File

@ -0,0 +1,37 @@
using System;
using System.Collections;
using System.Globalization;
using Avalonia.Data.Converters;
namespace XSDVisualiser.Desktop.Converters
{
/// <summary>
/// Returns true if the bound value is a collection with at least one item. Supports IEnumerable and ICollection.
/// Optional parameter "Invert" to invert the boolean result.
/// </summary>
public sealed class CollectionHasItemsConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
var hasItems = false;
switch (value)
{
case ICollection coll:
hasItems = coll.Count > 0;
break;
case IEnumerable enumerable:
{
var enumerator = enumerable.GetEnumerator();
hasItems = enumerator.MoveNext();
break;
}
}
var invert = parameter is string s && s.Equals("Invert", StringComparison.OrdinalIgnoreCase);
return invert ? !hasItems : hasItems;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
}

View File

@ -9,6 +9,7 @@
<conv:CardinalityToLabelConverter x:Key="CardinalityToLabel"/>
<conv:OptionalToBorderBrushConverter x:Key="OptionalToBrush"/>
<conv:OptionalToThicknessConverter x:Key="OptionalToThickness"/>
<conv:CollectionHasItemsConverter x:Key="HasItems"/>
</Window.Resources>
<Grid RowDefinitions="Auto,*" Margin="12">
@ -129,7 +130,7 @@
<StackPanel>
<TextBlock Text="Enumerations" FontWeight="SemiBold"/>
<ItemsControl ItemsSource="{Binding Enumerations}">
<ItemsControl ItemsSource="{Binding Enumerations}" IsVisible="{Binding Enumerations, Converter={StaticResource HasItems}}">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="x:String">
<TextBlock Text="{Binding ., StringFormat=• {0}}"/>
@ -140,7 +141,7 @@
<StackPanel>
<TextBlock Text="Patterns" FontWeight="SemiBold"/>
<ItemsControl ItemsSource="{Binding Patterns}">
<ItemsControl ItemsSource="{Binding Patterns}" IsVisible="{Binding Patterns, Converter={StaticResource HasItems}}">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="x:String">
<TextBlock Text="{Binding ., StringFormat=• {0}}"/>