为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
肥皂起泡泡 2022-11-21 16:25:04
见下图,如何将下拉列表边框与实际的组合框对齐?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" />
查看完整描述

1 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

我认为问题是 theComboBox不是 theTemplatedParentComboBoxItem(实际上有人指出,例如在这个评论中),所以最好明确地搜索它:

<Setter Property="Width" Value="{Binding 
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=ActualWidth}" />


查看完整回答
反对 回复 2022-11-21
  • 1 回答
  • 0 关注
  • 65 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信