样式扩展器控制模板,无法在单击时查看扩展内容

时间:2015-12-12 19:05:39

标签: c# wpf xaml controltemplate expander

我只是在WPF中设计扩展器控件。我只定义了Expander Control Template中的样式,但是当我点击它时,我无法查看扩展器的内容。 我想我还要定义扩展器的分支?但我不知道哪个部落以及如何定义它。 另外,为什么我只需要在扩展器样式化时定义触发器。

<Window x:Class="ExpanderControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="Expander">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Expander}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="30"></RowDefinition>
                                <RowDefinition Height="0" Name="contentRow"></RowDefinition>
                            </Grid.RowDefinitions>
                            <!--Expander Header-->
                            <Border Background="AliceBlue"
                                    Grid.Row="0"
                                    >
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                        <ColumnDefinition Width="20"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <ContentPresenter Grid.Column="0" ContentSource="Header"
                                                      RecognizesAccessKey="True"
                                                      VerticalAlignment="Center"
                                                      HorizontalAlignment="Left"
                                                      Margin="5"
                                                      ></ContentPresenter>
                                    <ToggleButton Grid.Column="1">
                                        <TextBlock>x</TextBlock>
                                    </ToggleButton>
                                </Grid>
                            </Border>

                            <!--Expander Content-->
                            <Border Background="Aqua" Grid.Row="1">
                                <ContentPresenter  Grid.Row="1"></ContentPresenter>
                            </Border>
                        </Grid>


                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Expander Header="Expander" HorizontalAlignment="Left" Margin="205,95,0,0" VerticalAlignment="Top" Width="200">
            <Grid Background="#FFE5E5E5">
                <Menu>
                    <MenuItem Header="hi"></MenuItem>
                </Menu>

            </Grid>
        </Expander>

    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

目前没有与ToggleButton相关联的操作。您需要通过绑定来使用Expander.IsExpanded属性

  1. ToggleButton.IsChecked
  2. Border.Visibility通过BooleanToVisibilityConverter(自定义或内置)
  3. 将内容行高设置为Auto
  4. 这样更改ToggleButton.IsChecked会更改Expander.IsExpanded,这反过来会影响内容Border的可见性。当您从外部更改IsExpanded属性时,它也会起作用。

    这是有效的XAML

    <Window.Resources>
       <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
       <Style TargetType="{x:Type Expander}">
          <Setter Property="Template">
             <Setter.Value>
                <ControlTemplate TargetType="{x:Type Expander}">
                   <Grid>
                      <Grid.RowDefinitions>
                         <RowDefinition Height="30"/>
                         <RowDefinition Height="Auto" Name="contentRow"/>
                      </Grid.RowDefinitions>
                      <!--Expander Header-->
                      <Border Background="AliceBlue" Grid.Row="0">
                         <Grid>
                            <Grid.ColumnDefinitions>
                               <ColumnDefinition Width="*"/>
                               <ColumnDefinition Width="20"/>
                            </Grid.ColumnDefinitions>
                            <ContentPresenter Grid.Column="0" ContentSource="Header" RecognizesAccessKey="True" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5" />
                            <ToggleButton Grid.Column="1" Content="x" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsExpanded}"/>
                         </Grid>
                      </Border>
                      <!--Expander Content-->
                      <Border Background="Aqua" Grid.Row="1" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsExpanded, Converter={StaticResource BooleanToVisibilityConverter}}">
                         <ContentPresenter/>
                      </Border>
                   </Grid>
                </ControlTemplate>
             </Setter.Value>
          </Setter>
       </Style>
    </Window.Resources>
    

    修改

    如果您希望整个标头能够展开/折叠Expander,则需要ContentPresenter Header Content ToggleButton Grid。在您的情况下,基本上将标题ToggleButton.Content带入<!--Expander Header--> <ToggleButton IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsExpanded}" HorizontalContentAlignment="Stretch" Grid.Row="0"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <ContentPresenter Grid.Column="0" ContentSource="Header" RecognizesAccessKey="True" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5" /> <TextBlock Grid.Column="1" Text="x" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> </ToggleButton>

    .CSV