Fix MultiComboBox to works with items added in axaml

This commit is contained in:
Samuel Bergeron-Drouin
2025-08-18 15:08:42 -04:00
parent 88318336a4
commit 976ddc4a5e
2 changed files with 51 additions and 28 deletions

View File

@@ -11,37 +11,52 @@
x:CompileBindings="True"
x:DataType="vm:MultiComboBoxDemoViewModel"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical" Spacing="10">
<TextBlock>Binded ItemsSource</TextBlock>
<StackPanel Orientation="Horizontal">
<u:MultiComboBox
Watermark="Please Select"
Width="300"
MaxHeight="200"
SelectedItems="{Binding SelectedItems}"
ItemsSource="{Binding Items}" >
</u:MultiComboBox>
<u:MultiComboBox
Name="combo"
Watermark="Please Select"
Width="300"
InnerLeftContent="Left"
InnerRightContent="Right"
Classes="ClearButton"
MaxHeight="200"
SelectedItems="{Binding SelectedItems}"
ItemsSource="{Binding Items}" >
<u:MultiComboBox.PopupInnerTopContent>
<StackPanel Margin="0" Orientation="Horizontal">
<Button Theme="{DynamicResource BorderlessButton}" Content="Select All" Command="{Binding SelectAllCommand}"/>
<Button Theme="{DynamicResource BorderlessButton}" Content="Unselect All" Command="{Binding ClearAllCommand}"/>
<Button Theme="{DynamicResource BorderlessButton}" Content="Inverse" Command="{Binding InvertSelectionCommand}"/>
</StackPanel>
</u:MultiComboBox.PopupInnerTopContent>
<u:MultiComboBox.ContextFlyout>
<MenuFlyout>
<MenuItem Header="Select All" Command="{Binding SelectAllCommand}"/>
</MenuFlyout>
</u:MultiComboBox.ContextFlyout>
</u:MultiComboBox>
<ListBox ItemsSource="{Binding SelectedItems}" />
</StackPanel>
<TextBlock>Items inside axaml (u:MultiComboBox's tag content)</TextBlock>
<u:MultiComboBox
Watermark="Please Select"
Width="300"
MaxHeight="200"
SelectedItems="{Binding SelectedItems}"
ItemsSource="{Binding Items}" >
MaxHeight="200">
<u:MultiComboBoxItem>option 1</u:MultiComboBoxItem>
<u:MultiComboBoxItem>option 2</u:MultiComboBoxItem>
<u:MultiComboBoxItem>option 3</u:MultiComboBoxItem>
</u:MultiComboBox>
<u:MultiComboBox
Name="combo"
Watermark="Please Select"
Width="300"
InnerLeftContent="Left"
InnerRightContent="Right"
Classes="ClearButton"
MaxHeight="200"
SelectedItems="{Binding SelectedItems}"
ItemsSource="{Binding Items}" >
<u:MultiComboBox.PopupInnerTopContent>
<StackPanel Margin="0" Orientation="Horizontal">
<Button Theme="{DynamicResource BorderlessButton}" Content="Select All" Command="{Binding SelectAllCommand}"/>
<Button Theme="{DynamicResource BorderlessButton}" Content="Unselect All" Command="{Binding ClearAllCommand}"/>
<Button Theme="{DynamicResource BorderlessButton}" Content="Inverse" Command="{Binding InvertSelectionCommand}"/>
</StackPanel>
</u:MultiComboBox.PopupInnerTopContent>
<u:MultiComboBox.ContextFlyout>
<MenuFlyout>
<MenuItem Header="Select All" Command="{Binding SelectAllCommand}"/>
</MenuFlyout>
</u:MultiComboBox.ContextFlyout>
</u:MultiComboBox>
<ListBox ItemsSource="{Binding SelectedItems}" />
</StackPanel>
</UserControl>

View File

@@ -178,6 +178,14 @@ public class MultiComboBox : SelectingItemsControl, IInnerContentControl, IPopup
{
return new MultiComboBoxItem();
}
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
{
if (item is MultiComboBoxItem containerItem)
{
container.DataContext = containerItem.Content;
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{