Skip to content

Commit

Permalink
feat: use coerce instead of multi binding.
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitism committed Apr 18, 2024
1 parent 8356bd8 commit 292a1db
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 50 deletions.
4 changes: 2 additions & 2 deletions samples/ControlCatalog/Pages/ComboBoxPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.SelectedItemTemplate>
<ComboBox.SelectionBoxItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Foreground="Red"></TextBlock>
</DataTemplate>
</ComboBox.SelectedItemTemplate>
</ComboBox.SelectionBoxItemTemplate>
</ComboBox>

<ComboBox WrapSelection="{Binding WrapSelection}" ItemsSource="{Binding Values}" >
Expand Down
29 changes: 21 additions & 8 deletions src/Avalonia.Controls/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,21 @@ public class ComboBox : SelectingItemsControl
ContentControl.VerticalContentAlignmentProperty.AddOwner<ComboBox>();

/// <summary>
/// Defines the <see cref="SelectedItemTemplate"/> property.
/// Defines the <see cref="SelectionBoxItemTemplate"/> property.
/// </summary>
public static readonly StyledProperty<IDataTemplate?> SelectedItemTemplateProperty =
public static readonly StyledProperty<IDataTemplate?> SelectionBoxItemTemplateProperty =
AvaloniaProperty.Register<ComboBox, IDataTemplate?>(
nameof(SelectedItemTemplate), defaultBindingMode: BindingMode.TwoWay);
nameof(SelectionBoxItemTemplate), defaultBindingMode: BindingMode.TwoWay, coerce: CoerceSelectionBoxItemTemplate);

private static IDataTemplate? CoerceSelectionBoxItemTemplate(AvaloniaObject obj, IDataTemplate? template)
{
if (template is not null) return template;
if(obj is ComboBox comboBox && template is null)
{
return comboBox.ItemTemplate;
}
return template;
}

private Popup? _popup;
private object? _selectionBoxItem;
Expand Down Expand Up @@ -172,10 +182,10 @@ public VerticalAlignment VerticalContentAlignment
/// Gets or sets the DataTemplate used to display the selected item. This has a higher priority than <see cref="ItemsControl.ItemTemplate"/> if set.
/// </summary>
[InheritDataTypeFromItems(nameof(ItemsSource))]
public IDataTemplate? SelectedItemTemplate
public IDataTemplate? SelectionBoxItemTemplate
{
get => GetValue(SelectedItemTemplateProperty);
set => SetValue(SelectedItemTemplateProperty, value);
get => GetValue(SelectionBoxItemTemplateProperty);
set => SetValue(SelectionBoxItemTemplateProperty, value);
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
Expand Down Expand Up @@ -341,7 +351,10 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
{
PseudoClasses.Set(pcDropdownOpen, change.GetNewValue<bool>());
}

else if (change.Property == ItemTemplateProperty)
{
CoerceValue(SelectionBoxItemTemplateProperty);
}
base.OnPropertyChanged(change);
}

Expand Down Expand Up @@ -452,7 +465,7 @@ private void UpdateSelectionBoxItem(object? item)
}
else
{
if(ItemTemplate is null && SelectedItemTemplate is null && DisplayMemberBinding is { } binding)
if(ItemTemplate is null && SelectionBoxItemTemplate is null && DisplayMemberBinding is { } binding)
{
var template = new FuncDataTemplate<object?>((_, _) =>
new TextBlock
Expand Down
23 changes: 0 additions & 23 deletions src/Avalonia.Controls/Converters/MultiDataTemplatesConverter.cs

This file was deleted.

9 changes: 1 addition & 8 deletions src/Avalonia.Themes.Fluent/Controls/ComboBox.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="using:System"
xmlns:converters="clr-namespace:Avalonia.Controls.Converters;assembly=Avalonia.Controls"
x:ClassModifier="internal">
<Design.PreviewWith>
<Border Padding="20">
Expand Down Expand Up @@ -37,7 +36,6 @@
<Thickness x:Key="ComboBoxPadding">12,5,0,7</Thickness>
<Thickness x:Key="ComboBoxEditableTextPadding">11,5,32,6</Thickness>
<x:Double x:Key="ComboBoxMinHeight">32</x:Double>
<converters:MultiDataTemplatesConverter x:Key="TemplatesConverter"/>

<ControlTheme x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="Padding" Value="{DynamicResource ComboBoxPadding}" />
Expand Down Expand Up @@ -89,13 +87,8 @@
Grid.Column="0"
Margin="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
<ContentControl.ContentTemplate>
<MultiBinding Converter="{StaticResource TemplatesConverter}">
<TemplateBinding Property="SelectedItemTemplate"></TemplateBinding>
<TemplateBinding Property="ItemTemplate"></TemplateBinding>
</MultiBinding>
</ContentControl.ContentTemplate>
</ContentControl>

<Border x:Name="DropDownOverlay"
Expand Down
11 changes: 2 additions & 9 deletions src/Avalonia.Themes.Simple/Controls/ComboBox.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:Avalonia.Controls.Converters;assembly=Avalonia.Controls"
x:ClassModifier="internal">
<converters:MultiDataTemplatesConverter x:Key="TemplatesConverter"/>
<ControlTheme x:Key="{x:Type ComboBox}"
TargetType="ComboBox">
<Setter Property="Background" Value="Transparent" />
Expand Down Expand Up @@ -35,13 +33,8 @@
<ContentControl Margin="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding SelectionBoxItem}">
<ContentControl.ContentTemplate>
<MultiBinding Converter="{StaticResource TemplatesConverter}">
<TemplateBinding Property="SelectedItemTemplate"></TemplateBinding>
<TemplateBinding Property="ItemTemplate"></TemplateBinding>
</MultiBinding>
</ContentControl.ContentTemplate>
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}">
</ContentControl>
<ToggleButton Name="toggle"
Grid.Column="1"
Expand Down

0 comments on commit 292a1db

Please sign in to comment.