English 中文(简体)
How to make ComboBox s items layout take full width in UWP
原标题:

I have the following ComboBox with a custom DataTemplate for the combobox s ItemTemplate:

                    
<ComboBox SelectedIndex="{x:Bind PaletteType.GetHashCode()}"
                              x:Name="PaletteComboBox" MinWidth="200"
                              ItemsSource="{x:Bind ComboPalettes}"
                              SelectionChanged="PaletteComboBox_OnSelectionChanged">
    <ComboBox.ItemTemplate>
        <DataTemplate x:DataType="adapters:ComboPaletteAdapter">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                    <TextBlock Text="{x:Bind PaletteNameTranslatedResource}"
                                               HorizontalAlignment="Left"
                                               VerticalAlignment="Center"
                                               Margin="0,0,0,0" />
                    <StackPanel Orientation="Horizontal"
                                                HorizontalAlignment="Right"
                                                VerticalAlignment="Center" Spacing="8"
                                                Margin="0,4,0,0">
                        <Border Width="24"
                                                Height="24"
                                                Background="{x:Bind CurrentPalette.ColorRed, Converter={StaticResource ColorToBrushConverter}, Mode=OneWay}"
                                                CornerRadius="26"
                                                IsTapEnabled="False"
                                                x:Uid="SettingsPaletteColorRedTT" />
                        <Border Width="24"
                                                Height="24"
                                                Background="{x:Bind CurrentPalette.ColorOrange, Converter={StaticResource ColorToBrushConverter}, Mode=OneWay}"
                                                CornerRadius="26"
                                                IsTapEnabled="False"
                                                x:Uid="SettingsPaletteColorOrangeTT" />
                        <Border Width="24"
                                                Height="24"
                                                Background="{x:Bind CurrentPalette.ColorGreen, Converter={StaticResource ColorToBrushConverter}, Mode=OneWay}"
                                                CornerRadius="26"
                                                IsTapEnabled="False"
                                                x:Uid="SettingsPaletteColorGreenTT" />
                        <Border Width="24"
                                                Height="24"
                                                Background="{x:Bind CurrentPalette.ColorBlue, Converter={StaticResource ColorToBrushConverter}, Mode=OneWay}"
                                                CornerRadius="26"
                                                IsTapEnabled="False"
                                                x:Uid="SettingsPaletteColorBlueTT" />
                    </StackPanel>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

I want to add a text to the left of the color borders, while aligning the colors to the right, but doesn t seem that HorizontalAlignment="Stretch" works.

The items space taken is always based on its content: enter image description here

How can I make every item take full width of the combobox s dropdown menu?

问题回答

It s not pretty. But it works. I don t know any other way to access visual СomboBox items.

<ComboBox Name="ComboPalettesComboBox"
          ItemsSource="{Binding ComboPalettes}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid Loaded="ItemGrid_OnLoaded">
                    <DockPanel>
                        <TextBlock Text="{Binding}"/>
                        <StackPanel Orientation="Horizontal"
                                    HorizontalAlignment="Right">
                            <StackPanel.Resources>
                                <Style TargetType="Border">
                                    <Setter Property="Width" Value="24"/>
                                    <Setter Property="Height" Value="24"/>
                                    <Setter Property="CornerRadius" Value="26"/>
                                </Style>
                            </StackPanel.Resources>
                            <Border Background="Red"/>
                            <Border Background="Orange"/>
                            <Border Background="Green"/>
                            <Border Background="Blue"/>
                        </StackPanel>
                    </DockPanel>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
private readonly List<Grid> _gridList = new List<Grid>();
    private bool _gridListIsFull;
    private double _maxGridWidth;

    public MainWindow()
    {
        InitializeComponent();

        Loaded += (sender, args) =>
        {
            Popup popup = ComboPalettesComboBox.Template.FindName("PART_Popup", ComboPalettesComboBox) as Popup;

            popup.Opened += (o, args2) =>
            {
                foreach (Grid grid in _gridList)
                {
                    grid.Width = _maxGridWidth;
                }

                _gridListIsFull = true;
            };
        };
    }

    private void ItemGrid_OnLoaded(object sender, RoutedEventArgs e)
    {
        if (_gridListIsFull)
        {
            _gridList.Clear();
            _gridListIsFull = false;
            _maxGridWidth = 0;
        }

        Grid grid = (Grid)sender;

        if (grid.ActualWidth > _maxGridWidth)
        {
            _maxGridWidth = grid.ActualWidth;
        }

        _gridList.Add(grid);
    }

How to make ComboBox s items layout take full width in UWP

The reason for the behavior you got is that you are setting the Width of the column as Auto. Please just use * instead.

Like this:

 <Grid.ColumnDefinitions>
 <ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>

Then if you want to make all the color align to right, you might use Grid instead of Stackpanel

Here is the sample code that I use:

        <ComboBox x:Name="PaletteComboBox" MinWidth="200"
                          ItemsSource="{x:Bind ComboPalettes}">
        <ComboBox.ItemTemplate>
            <DataTemplate >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid Background="Yellow"   >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding }"
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Center"
                                   Grid.Column="0"
                                   Margin="0,0,0,0" />
                        <StackPanel Orientation="Horizontal"
                                    Spacing="8"
                                    Grid.Column="1"
                                    HorizontalAlignment="Right"
                                    Margin="0,4,0,0">
                            <Border Width="24"
                                    Height="24"
                                    Background="Red"
                                    CornerRadius="26"
                                    IsTapEnabled="False"
                                    x:Uid="SettingsPaletteColorRedTT" />
                            <Border Width="24" 
                                    Height="24"
                                    Background="Gray"
                                    CornerRadius="26"
                                    IsTapEnabled="False"
                                    x:Uid="SettingsPaletteColorOrangeTT" />
                            <Border Width="24" 
                                    Height="24" 
                                    Background="Green" 
                                    CornerRadius="26"
                                    IsTapEnabled="False"
                                    x:Uid="SettingsPaletteColorGreenTT" />
                            <Border Width="24"
                                    Height="24"
                                    Background="Blue"
                                    CornerRadius="26"
                                    IsTapEnabled="False"
                                    x:Uid="SettingsPaletteColorBlueTT" />
                        </StackPanel>
                    </Grid>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

The result looks like:

enter image description here





相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签