How to align WPF Expander control toggle button

笑着哭i 提交于 2020-01-02 03:44:09

问题


Hi I was wondering is it possible to align the toggle button on a WPF expander control to the far right side?


回答1:


With WPF all things are possible. ;) Unfortunately not all things are simple. Your best bet here is to re-template the expander. Start out by copying the default Expander template, found here.

Next, find the Grid that contains 2 columns, one containing a ToggleButton and the other containing a ContentPresenter. Swap the columns so the toggle is in column 1. Then change the column definition sizes so the first column is star-sized, and the second is size 20. When finished, you should have a chunk in the template that looks like this:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="20" />
  </Grid.ColumnDefinitions>
    <ToggleButton Grid.Column="1"
      IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,
      RelativeSource={RelativeSource TemplatedParent}}"
      OverridesDefaultStyle="True" 
      Template="{StaticResource ExpanderToggleButton}" 
      Background="{StaticResource NormalBrush}" />
    <ContentPresenter Margin="4" 
      ContentSource="Header" 
      RecognizesAccessKey="True" />
</Grid>

Continue modifying the template until you get the look and feel that you need.

EDIT: The template provided on MSDN is a bare-bones version of the "real" expander template. If you want the stylized expander template, use Expression Blend and copy the existing control template off an Expander.



来源:https://stackoverflow.com/questions/2587683/how-to-align-wpf-expander-control-toggle-button

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