Positon of DropDown list of ComboBox in UWP

六眼飞鱼酱① 提交于 2019-12-11 11:13:28

问题


I have a ComboBox in my universal application, I want DropDown list open below of my combo not over it. how can I change position of DropDown list of ComboBox in UWP?


回答1:


The DropDown of a ComboBox is actually a Popup, and the position where to show this Popup is defined in the code behind, and we can't access to it. One workaround is finding this Popup and relocate it when it is opened, but using this method we need to calculate the VerticalOffset property each time when it is opened, and there are quite many scenarios for different value of VerticalOffset.

So my suggestion is design a custom control which behavior like a ComboBox, for example I created a UserControl like this:

<Button x:Name="rootButton" BorderBrush="Gray" BorderThickness="2" Click="Button_Click" MinWidth="80" Background="Transparent" Padding="0">
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
          Width="{Binding ElementName=rootButton, Path=ActualWidth}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="32" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="{x:Bind selectedItem, Mode=OneWay}" Grid.Column="0" VerticalAlignment="Center" FontSize="15" HorizontalAlignment="Center" />
        <FontIcon Grid.Column="1" FontSize="12" FontFamily="Segoe MDL2 Assets" Glyph="&#xE0E5;" HorizontalAlignment="Right"
               Margin="0,10,10,10" VerticalAlignment="Center" />
    </Grid>
    <FlyoutBase.AttachedFlyout>
        <MenuFlyout Placement="Bottom" x:Name="menuFlyout">
            <MenuFlyoutItem Text="Item 1" Click="MenuFlyoutItem_Click" />
            <MenuFlyoutItem Text="Item 2" Click="MenuFlyoutItem_Click" />
            <MenuFlyoutItem Text="Item 3" Click="MenuFlyoutItem_Click" />
            <MenuFlyoutItem Text="Item 4" Click="MenuFlyoutItem_Click" />
            <MenuFlyoutItem Text="Item 5" Click="MenuFlyoutItem_Click" />
        </MenuFlyout>
    </FlyoutBase.AttachedFlyout>
</Button>

and the code behind in this UserControl:

public sealed partial class CustomComboBox : UserControl, INotifyPropertyChanged
{
    public CustomComboBox()
    {
        this.InitializeComponent();
        selectedItem = "";
    }

    private string _selectedItem;

    public string selectedItem
    {
        get { return _selectedItem; }

        set
        {
            _selectedItem = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("selectedItem"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
    {
        var item = sender as MenuFlyoutItem;
        selectedItem = item.Text;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        FlyoutBase.ShowAttachedFlyout(sender as Button);
    }
}

And you can use this CustomComboBox in other page like this:

<local:CustomComboBox VerticalAlignment="Center" HorizontalAlignment="Center" />

By default this CustomComboBox will show its DropDown list under it, unless there is no enough space under it to hold this DropDown, in this case, the DropDown will be shown above this CustomComboBox.



来源:https://stackoverflow.com/questions/36516439/positon-of-dropdown-list-of-combobox-in-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!