@DaisyTian-MSFT, altogether it's 18 headers so Convert
is called 18 times and these 18 Convert
calls call your Recursive
33 times. So, just like my original if else
in foreach
, yours Recursive
also sets Foreground
and FontSize
more times than necessary! One way to reduce is to use break
in my original Convert
like this:
public class HeaderConverter : IValueConverter
{
int convert = 0;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var group = value as CollectionViewGroup;
var header = new TextBlock() { Text = group.Name.ToString(), FontWeight = FontWeights.Bold };
if ((!group.IsBottomLevel))
{
foreach (CollectionViewGroup subGroup in group.Items)
{
if (subGroup.IsBottomLevel)
{
Debug.WriteLine(++convert);
header.Foreground = Brushes.Blue;
header.FontSize = 12;
}
else
{
foreach (CollectionViewGroup sSubGroup in subGroup.Items)
{
if (sSubGroup.IsBottomLevel)
{
Debug.WriteLine(++convert);
header.Foreground = Brushes.Green;
header.FontSize = 14;
}
else
{
Debug.WriteLine(++convert);
header.Foreground = Brushes.Red;
header.FontSize = 16;
header.Margin = new Thickness(0, 10, 0, 0);
}
break;
}
}
break;
}
}
return header;
}
BUT still I've to use foreach
and if else
! It'd be nice If I'd access to the indexes of group levels and could set those styles based on the leve like this:
public class HeaderConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var group = value as CollectionViewGroup;
var header = new TextBlock() { Text = group.Name.ToString(), FontWeight = FontWeights.Bold };
switch (group.Level) // non existent property group.Level
{
case 0:
header.Foreground = Brushes.Red;
header.FontSize = 16;
header.Margin = new Thickness(0, 10, 0, 0);
break;
case 1:
header.Foreground = Brushes.Green;
header.FontSize = 14;
break;
case 2:
header.Foreground = Brushes.Blue;
header.FontSize = 12;
break;
}
return header;
}
or in xaml
with a DataTrigger
like this:
<ControlTemplate TargetType="GroupItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="header" Text="{Binding Name}"/>
<ItemsPresenter x:Name="items" Grid.Row="1" Margin="15 0 0 0"/>
<ContentControl x:Name="footer" Grid.Row="2" Content="{Binding Converter={StaticResource FC}}"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding GroupLevel}" Value="0">
<Setter Property="TextBlock.FontSize" Value="16" TargetName="header"/>
<Setter Property="TextBlock.Foreground" Value="Red" TargetName="header"/>
</DataTrigger>
<DataTrigger Binding="{Binding GroupLevel}" Value="1"> ... </DataTrigger>
<DataTrigger Binding="{Binding GroupLevel}" Value="2"> ... </DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>