C# 自定义组合框样式

C# 自定义组合框样式,c#,wpf,C#,Wpf,所以在学习WPF几天后,我尝试为我的组合框定制样式。我想用内容宽度设置边框,以填充边框和下拉按钮之间所有未使用的空间。在我解决这个问题后,我将从带有项目的下拉列表开始 结果: 代码: 在DockPanel中的边框前移动按钮,使其成为面板的最后一个子项 然后在DockPanel上将LastChildFill设置为True <DockPanel LastChildFill="True"> <Button x:Name="dropBtn" Width="20" DockPa

所以在学习WPF几天后,我尝试为我的组合框定制样式。我想用内容宽度设置边框,以填充边框和下拉按钮之间所有未使用的空间。在我解决这个问题后,我将从带有项目的下拉列表开始

结果:

代码:

在DockPanel中的边框前移动按钮,使其成为面板的最后一个子项

然后在DockPanel上将
LastChildFill
设置为
True

<DockPanel LastChildFill="True">
   <Button x:Name="dropBtn" Width="20" DockPanel.Dock="Right" 
           HorizontalAlignment="Right">
       <Path VerticalAlignment="Center"
             Width="10"
             Height="8"
             Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
             Stretch="Fill"
             Fill="HotPink"/>
   </Button>
   <Border x:Name="bg" BorderBrush="Yellow" Background="HotPink"
           BorderThickness="1" CornerRadius="0" DockPanel.Dock="Left">
      <ContentPresenter
             Name="ContentSite"
             IsHitTestVisible="False" 
             Content="{TemplateBinding SelectionBoxItem}"
             ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
             ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
             Margin="3,3,23,3"
             VerticalAlignment="Center"
             HorizontalAlignment="Left"/>
   </Border>
</DockPanel>

根据以下文件:

获取或设置一个值,该值指示最后一个子元素 在DockPanel中,拉伸以填充剩余的可用空间


我已经编辑了你的标题。请看,“,其中的共识是“不,他们不应该”。@JohnSaunders我会记住这一点。谢谢
<DockPanel LastChildFill="True">
   <Button x:Name="dropBtn" Width="20" DockPanel.Dock="Right" 
           HorizontalAlignment="Right">
       <Path VerticalAlignment="Center"
             Width="10"
             Height="8"
             Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
             Stretch="Fill"
             Fill="HotPink"/>
   </Button>
   <Border x:Name="bg" BorderBrush="Yellow" Background="HotPink"
           BorderThickness="1" CornerRadius="0" DockPanel.Dock="Left">
      <ContentPresenter
             Name="ContentSite"
             IsHitTestVisible="False" 
             Content="{TemplateBinding SelectionBoxItem}"
             ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
             ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
             Margin="3,3,23,3"
             VerticalAlignment="Center"
             HorizontalAlignment="Left"/>
   </Border>
</DockPanel>