首页 文章

C#.Net WPF自定义用户控件zIndex问题与类似的用户控件

提问于
浏览
0

用户控制细节:

创建了下拉列表控件(如组合框),单击向下箭头按钮,它会在文本框下方显示一个列表

我已设置用户控件的zIndex属性

问题:

情况1:当有另一个用户控件(我的自定义用户控件除外),并且如果显示下拉列表时,其他用户控件隐藏在我的用户控件之后 . 这完全没问题

情况2:有2个自定义用户控件,如果从第一个用户控件显示列表,则第二个用户控件出现在列表中 . 这是我面临问题的地方

我控制的XAML如下

<UserControlx:Class="UserControls.AutoCompleteComboBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Panel.ZIndex="1110" LostFocus="UserControl_LostFocus" Height="Auto">

        <Canvas Name="MainCanvas">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" MinWidth="150"></ColumnDefinition>
                    <ColumnDefinition Width="20"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBox Name="autoTextBox" Height="20" MinWidth="150" Width="Auto" MinHeight="20" Style="{DynamicResource AutoCompleteBox}" BorderThickness="2"
                     Margin="0,0,0,0" TextWrapping="NoWrap"  Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
                <Button Content="6" FontFamily="Marlett" Grid.Row="0" Grid.Column="1" FontSize="15" Margin="0,0,0,0" Height="20" Width="20" HorizontalAlignment="Right" VerticalAlignment="Top" Background="{StaticResource BackgroudBlueBrush}"  Click="Button_Click" Padding="0" Cursor="Hand"></Button>
                <StackPanel Grid.Row="1" Grid.ColumnSpan="2" >
                    <ListBox Name="suggestionListBox" SelectionChanged="suggestionListBox_SelectionChanged" MouseDown="suggestionListBox_MouseDown"
                     Background="LightYellow" SnapsToDevicePixels="True"
                     Visibility="Collapsed" 
                     MinWidth="150" IsHitTestVisible="True" MinHeight="70" Height="70"
                     VerticalAlignment="Top" LostFocus="suggestionListBox_LostFocus"/>
                </StackPanel>
            </Grid>
        </Canvas>
</UserControl>

2 回答

  • 0

    您的方法不是正确管理控件重叠的方法 . 也许您可以使用ZIndex属性创建一些技巧,但这不是解决方案 .

    如果你需要一个下拉控件,最好的方法是使用一个Popup控件并围绕它进行游戏 . 基本上,它创造了另一个无边界窗口,是你的孩子 .

    另一种方法,可能更简单,但不如Popup,使用Adorner . 也许这个是与你的最相似的技术 .

    干杯

  • 0

    您是否尝试将StackPanel的ZIndex设置为控件的zindex?这应该将下拉部分提升到用户控件的任何其他实例之上 .

    Canvas.ZIndex可以在StackPanels上使用 .

相关问题