如何将Combobox下拉列表的宽度设置为它所属的Combobox?

见下图,如何将下拉列表边框与实际的组合框对齐?


https://imgur.com/uNO45F2


这是 Combobox 的代码,它使用自定义的 ComboBoxItem 样式,


<ComboBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding ASDevicesView, Mode=OneWay , UpdateSourceTrigger=PropertyChanged}"  AutomationProperties.AutomationId="4314"

                      SelectedItem="{Binding SDevice}" IsEditable="True" Text="{Binding SearchText}" MaxDropDownHeight="166" ItemContainerStyle="{StaticResource MyComboBoxItemStyle}">

    <ComboBox.Style>

        <Style TargetType="{x:Type ComboBox}">

            <Style.Triggers>

                <Trigger Property="IsKeyboardFocusWithin" Value="True">

                    <Setter Property="IsDropDownOpen" Value="true" />

                </Trigger>

            </Style.Triggers>

        </Style>

    </ComboBox.Style>

</ComboBox>

这是 ComboBoxItem 样式的代码,


<Style x:Key="MyComboBoxItemStyle" BasedOn="{StaticResource {x:Type ComboBoxItem}}" TargetType="{x:Type ComboBoxItem}">

    <Setter Property="HorizontalContentAlignment" Value="Center" />

    <Setter Property="VerticalContentAlignment" Value="Center" />

    <Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" />

    <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}" />

    <Setter Property="Height" Value="40" />


肥皂起泡泡
浏览 66回答 1
1回答

开满天机

我认为问题是 theComboBox不是 theTemplatedParent的ComboBoxItem(实际上有人指出,例如在这个评论中),所以最好明确地搜索它:<Setter&nbsp;Property="Width"&nbsp;Value="{Binding&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;RelativeSource={RelativeSource&nbsp;FindAncestor,&nbsp;AncestorType={x:Type&nbsp;ComboBox}},&nbsp;Path=ActualWidth}"&nbsp;/>
打开App,查看更多内容
随时随地看视频慕课网APP