WPF Border for ComboBox

◇◆丶佛笑我妖孽 提交于 2020-01-05 10:05:01

问题


I try to use a ComboBox based on ToolBar.ComboBoxStyleKey. It has a flat design. Control by default does not have a border, i.e., on a white background is only visible icon of ToggleButton. How can I add border for control when focus isn't on ComboBox?

Thanks!

No focus:

Mouse hover:


回答1:


There is a Border control in WPF, you could use that to wrap around your ComboBox.

<Border Background="GhostWhite" BorderBrush="Gainsboro" BorderThickness="1">
    <ComboBox>
        ...
    </CombodBox>
</Border>

http://www.wpf-tutorial.com/misc-controls/the-border-control/

You could set the BorderThickness according to if the combobox has the focus or not.




回答2:


There are countless options available.

  1. Draw a rectangle around the combobox's (x,y) coordinates.

  2. Make your own custom combobox as shown in: CustomCombowithBorder.

  3. Add a panel behind it and give the panel a border thus imitating a border.

  4. etc....

EDIT:

Found that MSDN gives an excellent answer as also shown in ComboBox Styling .

<Setter Property="Template">
   <Setter.Value>
      <ControlTemplate TargetType="ComboBox">
         <Grid>
            <Border x:Name="ContentPresenterBorder">
               <Grid>
                  <ToggleButton x:Name="DropDownToggle"/>
                  <ContentPresenter x:Name="ContentPresenter" />
                     <TextBlock Text=" " />
                  </ContentPresenter>
               </Grid>
            </Border>
         </Grid>
      </ControlTemplate>
   </Setter.Value>
</Setter>


来源:https://stackoverflow.com/questions/31296098/wpf-border-for-combobox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!