【问题标题】:How to remove header space on ComboBox in XAML如何在 XAML 中删除 ComboBox 上的标题空间
【发布时间】:2015-06-30 15:58:50
【问题描述】:

我正在尝试在 StackPanel 内排列 TextBox 和 ComboBox,但 ComboBox 顶部似乎有一些我无法摆脱的额外空间。

如何让这两个控件正确对齐?

<StackPanel Orientation="Horizontal">
   <TextBox  IsTextPredictionEnabled="False" Margin="0,0,0,0" BorderBrush="Gray"               
        Text="{Binding ZoneName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
        PlaceholderText="YourCompany"/>
    <ComboBox VerticalAlignment="Stretch" BorderBrush="Gray" Padding="0,0,0,0" Margin="0,0,0,0"                      
         ItemsSource="{Binding Path=DomainChoices}"
         SelectedValue="{Binding Path=SelectedDomainChoice, Mode=TwoWay}" 
                    SelectedValuePath="{Binding id}" 
                     DisplayMemberPath="{Binding text}"
                      Foreground="Black" Width="200">
                <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding text}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
    </ComboBox>
</StackPanel>

【问题讨论】:

  • 你不能将 ComboBox 的边距设置为“0,-5,0,0”之类的东西
  • 我可以,但我宁愿消除引入额外空间的原因,而不是仅仅将整个控件向上移动。

标签: c# xaml combobox windows-phone-8.1 stackpanel


【解决方案1】:

当您查看ComboBox 的 样式时,您会发现&lt;Border x:Name="ShortListOuterBorder" ....Margin 值为Margin="{ThemeResource PhoneTouchTargetOverhang}"。该资源以上述样式定义:

<Thickness x:Key="PhoneTouchTargetOverhang">0,9.5</Thickness>

您可以轻松定义自己的样式,您可以在其中更改上述值,或更改边框的边距。您也可以尝试在不应用样式的情况下将 ComboBox 的边距设置为 0,-9.5,0,0

【讨论】:

  • 您能否更具体地说明如何覆盖 Border Margin 的值?我想我想将其设置为 0,0 而不是 0,9.5。我想避免将 ComboBox 边距设置为 0、-9.5 之类的变通方法,因为我宁愿解决根本问题,即边框边距。谢谢。
  • @iupchris10 打开设计器窗口(带有页面预览的窗口),右键单击您的 ComboBox -> 编辑模板 -> 编辑副本。这应该将默认样式添加到您的页面资源中。在那里编辑定义的资源,然后将样式应用于组合框(如果您以这种方式编辑,VS 应该会自动执行)。
猜你喜欢
  • 2019-12-10
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 1970-01-01
  • 2018-01-19
  • 2021-01-22
  • 1970-01-01
相关资源
最近更新 更多