views:

2484

answers:

2

Hi,

I have Multiple Texblock in Expander Header. I want these TextBlocks aligned to Left, Right and Center.

How can i achieve this?

Please Help...

Thanks

Sharath

The Text in Red should be left aligned, the one in blue should be center aligned and the green should be right aligned

Please copy the code in XAMLPad.Exe






+3  A: 

The reason that you can't simply use a Grid or Dock panel with the TextBlocks in to control the alignment is because the default template of Expander left aligns the header content instead of allowing it to stretch.

There are two ways around this. You can either specify a width for the content of the Expander's header or you can define a new template for the expander.

The second option is probably the best, but it is also the harder one to implement. If you have Expression Blend then you can right click the Expander element in the Objects and Timeline tree and select Edit Control Parts -> Edit a copy... Once you have done that you will need to modify the HeaderSite's template as well (you can select Edit Template this time though instead of creating a copy). Once your in there you can pretty much just set it to Stretch for horizontal alignment and add a bit of margin to the right and your done.

If you don't have Expression Blend update the question to specify that and I will post some sample code. The main reason I haven't done that here is that the template is about 250 lines of code and I didn't want to generate alot of noise for no reason.

Given the reply that you gave I am guessing you don't have Blend. If you put the following XAML into XAML Pad you should see an example of an Expander which will enable the header content to span the full width of the Expander. You will most likely want to move the resources for the page into a seperate resource dictionary so you can reuse it throught the application.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      >
      <Page.Resources>
     <LinearGradientBrush x:Key="ExpanderArrowHoverFill" EndPoint="0,1" StartPoint="0,0">
      <GradientStop Color="#FFF0F8FE" Offset="0"/>
      <GradientStop Color="#FFE0F3FE" Offset="0.3"/>
      <GradientStop Color="#FF6FA7C5" Offset="1"/>
     </LinearGradientBrush>
     <LinearGradientBrush x:Key="ExpanderArrowPressedFill" EndPoint="0,1" StartPoint="0,0">
      <GradientStop Color="#FFDCF0FA" Offset="0"/>
      <GradientStop Color="#FFC5E6F7" Offset="0.2"/>
      <GradientStop Color="#FF5690D0" Offset="1"/>
     </LinearGradientBrush>
     <LinearGradientBrush x:Key="ExpanderArrowFill" EndPoint="0,1" StartPoint="0,0">
      <GradientStop Color="White" Offset="0"/>
      <GradientStop Color="#FFBFBFBF" Offset="0.5"/>
      <GradientStop Color="#FF878787" Offset="1"/>
     </LinearGradientBrush>
     <Style x:Key="ExpanderRightHeaderStyle" TargetType="{x:Type ToggleButton}">
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
         <Border Padding="{TemplateBinding Padding}">
          <Grid SnapsToDevicePixels="False" Background="Transparent">
           <Grid.RowDefinitions>
            <RowDefinition Height="19"/>
            <RowDefinition Height="*"/>
           </Grid.RowDefinitions>
           <Grid>
            <Grid.LayoutTransform>
             <TransformGroup>
              <TransformGroup.Children>
               <TransformCollection>
                <RotateTransform Angle="-90"/>
               </TransformCollection>
              </TransformGroup.Children>
             </TransformGroup>
            </Grid.LayoutTransform>
            <Ellipse Fill="{DynamicResource ExpanderArrowFill}" Stroke="DarkGray" HorizontalAlignment="Center" x:Name="circle" VerticalAlignment="Center" Width="19" Height="19"/>
            <Path Stroke="#666" StrokeThickness="2" HorizontalAlignment="Center" x:Name="arrow" VerticalAlignment="Center" SnapsToDevicePixels="false" Data="M 1,1.5 L 4.5,5 L 8,1.5"/>
           </Grid>
           <ContentPresenter HorizontalAlignment="Center" Margin="0,4,0,0" VerticalAlignment="Top" SnapsToDevicePixels="True" Grid.Row="1" RecognizesAccessKey="True"/>
          </Grid>
         </Border>
         <ControlTemplate.Triggers>
          <Trigger Property="IsChecked" Value="true">
           <Setter Property="Data" TargetName="arrow" Value="M 1,4.5  L 4.5,1  L 8,4.5"/>
          </Trigger>
          <Trigger Property="IsMouseOver" Value="true">
           <Setter Property="Stroke" TargetName="circle" Value="#FF3C7FB1"/>
           <Setter Property="Fill" TargetName="circle" Value="{DynamicResource ExpanderArrowHoverFill}"/>
           <Setter Property="Stroke" TargetName="arrow" Value="#222"/>
          </Trigger>
          <Trigger Property="IsPressed" Value="true">
           <Setter Property="Stroke" TargetName="circle" Value="#FF526C7B"/>
           <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
           <Setter Property="Fill" TargetName="circle" Value="{DynamicResource ExpanderArrowPressedFill}"/>
           <Setter Property="Stroke" TargetName="arrow" Value="#FF003366"/>
          </Trigger>
         </ControlTemplate.Triggers>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>
     <Style x:Key="ExpanderUpHeaderStyle" TargetType="{x:Type ToggleButton}">
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
         <Border Padding="{TemplateBinding Padding}">
          <Grid SnapsToDevicePixels="False" Background="Transparent">
           <Grid.ColumnDefinitions>
            <ColumnDefinition Width="19"/>
            <ColumnDefinition Width="*"/>
           </Grid.ColumnDefinitions>
           <Grid>
            <Grid.LayoutTransform>
             <TransformGroup>
              <TransformGroup.Children>
               <TransformCollection>
                <RotateTransform Angle="180"/>
               </TransformCollection>
              </TransformGroup.Children>
             </TransformGroup>
            </Grid.LayoutTransform>
            <Ellipse Fill="{DynamicResource ExpanderArrowFill}" Stroke="DarkGray" HorizontalAlignment="Center" x:Name="circle" VerticalAlignment="Center" Width="19" Height="19"/>
            <Path Stroke="#666" StrokeThickness="2" HorizontalAlignment="Center" x:Name="arrow" VerticalAlignment="Center" SnapsToDevicePixels="false" Data="M 1,1.5 L 4.5,5 L 8,1.5"/>
           </Grid>
           <ContentPresenter HorizontalAlignment="Stretch" Margin="4,0,19,0" VerticalAlignment="Center" SnapsToDevicePixels="True" Grid.Column="1" RecognizesAccessKey="True"/>
          </Grid>
         </Border>
         <ControlTemplate.Triggers>
          <Trigger Property="IsChecked" Value="true">
           <Setter Property="Data" TargetName="arrow" Value="M 1,4.5  L 4.5,1  L 8,4.5"/>
          </Trigger>
          <Trigger Property="IsMouseOver" Value="true">
           <Setter Property="Stroke" TargetName="circle" Value="#FF3C7FB1"/>
           <Setter Property="Fill" TargetName="circle" Value="{DynamicResource ExpanderArrowHoverFill}"/>
           <Setter Property="Stroke" TargetName="arrow" Value="#222"/>
          </Trigger>
          <Trigger Property="IsPressed" Value="true">
           <Setter Property="Stroke" TargetName="circle" Value="#FF526C7B"/>
           <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
           <Setter Property="Fill" TargetName="circle" Value="{DynamicResource ExpanderArrowPressedFill}"/>
           <Setter Property="Stroke" TargetName="arrow" Value="#FF003366"/>
          </Trigger>
         </ControlTemplate.Triggers>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>
     <Style x:Key="ExpanderLeftHeaderStyle" TargetType="{x:Type ToggleButton}">
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
         <Border Padding="{TemplateBinding Padding}">
          <Grid SnapsToDevicePixels="False" Background="Transparent">
           <Grid.RowDefinitions>
            <RowDefinition Height="19"/>
            <RowDefinition Height="*"/>
           </Grid.RowDefinitions>
           <Grid>
            <Grid.LayoutTransform>
             <TransformGroup>
              <TransformGroup.Children>
               <TransformCollection>
                <RotateTransform Angle="90"/>
               </TransformCollection>
              </TransformGroup.Children>
             </TransformGroup>
            </Grid.LayoutTransform>
            <Ellipse Fill="{DynamicResource ExpanderArrowFill}" Stroke="DarkGray" HorizontalAlignment="Center" x:Name="circle" VerticalAlignment="Center" Width="19" Height="19"/>
            <Path Stroke="#666" StrokeThickness="2" HorizontalAlignment="Center" x:Name="arrow" VerticalAlignment="Center" SnapsToDevicePixels="false" Data="M 1,1.5 L 4.5,5 L 8,1.5"/>
           </Grid>
           <ContentPresenter HorizontalAlignment="Center" Margin="0,4,0,0" VerticalAlignment="Top" SnapsToDevicePixels="True" Grid.Row="1" RecognizesAccessKey="True"/>
          </Grid>
         </Border>
         <ControlTemplate.Triggers>
          <Trigger Property="IsChecked" Value="true">
           <Setter Property="Data" TargetName="arrow" Value="M 1,4.5  L 4.5,1  L 8,4.5"/>
          </Trigger>
          <Trigger Property="IsMouseOver" Value="true">
           <Setter Property="Stroke" TargetName="circle" Value="#FF3C7FB1"/>
           <Setter Property="Fill" TargetName="circle" Value="{DynamicResource ExpanderArrowHoverFill}"/>
           <Setter Property="Stroke" TargetName="arrow" Value="#222"/>
          </Trigger>
          <Trigger Property="IsPressed" Value="true">
           <Setter Property="Stroke" TargetName="circle" Value="#FF526C7B"/>
           <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
           <Setter Property="Fill" TargetName="circle" Value="{DynamicResource ExpanderArrowPressedFill}"/>
           <Setter Property="Stroke" TargetName="arrow" Value="#FF003366"/>
          </Trigger>
         </ControlTemplate.Triggers>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>
     <Style x:Key="ExpanderHeaderFocusVisual">
      <Setter Property="Control.Template">
       <Setter.Value>
        <ControlTemplate>
         <Border>
          <Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="0" SnapsToDevicePixels="true"/>
         </Border>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>
     <Style   x:Key="ExpanderDownHeaderStyle" TargetType="{x:Type ToggleButton}">
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
         <Border Padding="{TemplateBinding Padding}">
          <Grid SnapsToDevicePixels="False" Background="Transparent">
           <Grid.ColumnDefinitions>
            <ColumnDefinition Width="19"/>
            <ColumnDefinition Width="*"/>
           </Grid.ColumnDefinitions>
           <Ellipse Fill="{DynamicResource ExpanderArrowFill}" Stroke="DarkGray" HorizontalAlignment="Center" x:Name="circle" VerticalAlignment="Center" Width="19" Height="19"/>
           <Path Stroke="#666" StrokeThickness="2" HorizontalAlignment="Center" x:Name="arrow" VerticalAlignment="Center" SnapsToDevicePixels="false" Data="M 1,1.5 L 4.5,5 L 8,1.5"/>
           <ContentPresenter HorizontalAlignment="Stretch" Margin="4,0,19,0" VerticalAlignment="Center" SnapsToDevicePixels="True" Grid.Column="1" RecognizesAccessKey="True"/>
          </Grid>
         </Border>
         <ControlTemplate.Triggers>
          <Trigger Property="IsChecked" Value="true">
           <Setter Property="Data" TargetName="arrow" Value="M 1,4.5  L 4.5,1  L 8,4.5"/>
          </Trigger>
          <Trigger Property="IsMouseOver" Value="true">
           <Setter Property="Stroke" TargetName="circle" Value="#FF3C7FB1"/>
           <Setter Property="Fill" TargetName="circle" Value="{DynamicResource ExpanderArrowHoverFill}"/>
           <Setter Property="Stroke" TargetName="arrow" Value="#222"/>
          </Trigger>
          <Trigger Property="IsPressed" Value="true">
           <Setter Property="Stroke" TargetName="circle" Value="#FF526C7B"/>
           <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
           <Setter Property="Fill" TargetName="circle" Value="{DynamicResource ExpanderArrowPressedFill}"/>
           <Setter Property="Stroke" TargetName="arrow" Value="#FF003366"/>
          </Trigger>
         </ControlTemplate.Triggers>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>
     <Style x:Key="StretchExpanderStyle" TargetType="{x:Type Expander}">
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
      <Setter Property="Background" Value="Transparent"/>
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
      <Setter Property="VerticalContentAlignment" Value="Stretch"/>
      <Setter Property="BorderBrush" Value="Transparent"/>
      <Setter Property="BorderThickness" Value="1"/>
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="{x:Type Expander}">
         <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
          <DockPanel>
           <ToggleButton FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual}" Margin="1" MinHeight="0" MinWidth="0" x:Name="HeaderSite" Style="{StaticResource ExpanderDownHeaderStyle}" Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" DockPanel.Dock="Top" IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" x:Name="ExpandSite" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Focusable="false" Visibility="Collapsed" DockPanel.Dock="Bottom"/>
          </DockPanel>
         </Border>
         <ControlTemplate.Triggers>
          <Trigger Property="IsExpanded" Value="true">
           <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
          </Trigger>
          <Trigger Property="ExpandDirection" Value="Right">
           <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Right"/>
           <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Left"/>
           <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderRightHeaderStyle}"/>
          </Trigger>
          <Trigger Property="ExpandDirection" Value="Up">
           <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Top"/>
           <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Bottom"/>
           <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderUpHeaderStyle}"/>
          </Trigger>
          <Trigger Property="ExpandDirection" Value="Left">
           <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Left"/>
           <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Right"/>
           <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderLeftHeaderStyle}"/>
          </Trigger>
          <Trigger Property="IsEnabled" Value="false">
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
          </Trigger>
         </ControlTemplate.Triggers>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>
    </Page.Resources>
    <Grid>
        <Expander Width="500" Style="{DynamicResource StretchExpanderStyle}">
            <Expander.Header>
                <Grid>
                    <TextBlock DockPanel.Dock="Left" HorizontalAlignment="Left" Text="Left" Foreground="Red" />                                                                                      
                    <TextBlock Text="Center" HorizontalAlignment="Center" Foreground="Blue" />
                    <TextBlock DockPanel.Dock="Right" HorizontalAlignment="Right" Text="Right" Foreground="Green" />     
                </Grid>
                <!-- As an alternative you could use a dock panel. as shown by the following -->
                <!--<DockPanel HorizontalAlignment="Stretch">
                    <TextBlock DockPanel.Dock="Left" Text="Left" Foreground="Red" />                    
                    <TextBlock DockPanel.Dock="Right" Text="Right" Foreground="Green" />                                                   
                    <TextBlock Text="Center" HorizontalAlignment="Center" Foreground="Blue" />
                </DockPanel>
                -->                
            </Expander.Header>
            Some Content.......
        </Expander>
    </Grid>
</Page>
Caleb Vear
You didn't answer any question I had but I used your style after spending 2 hours trying to find a bug in my own. Thank you! Ups are on me!
Brad
A: 

Please copy the code in XAMLPad.Exe

The Text in Red should be left aligned, the one in blue should be center aligned and the green should be right aligned

Please reply.

Thanks

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <Grid>
  <Expander HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.ColumnSpan="8" Grid.Row="2">
                        <Expander.Header>
                            <TextBlock>
                             <InlineUIContainer> 
                                    <TextBlock Text="Summary" Foreground="Red" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" />
                            </InlineUIContainer>
                                  <Run Text=" "/> 
                              <InlineUIContainer>  
                                    <TextBlock Text="Duration:" TextWrapping="Wrap" Foreground="Blue" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                               </InlineUIContainer> 
                                 <Run Text=" "/> 
                                 <InlineUIContainer>  
                                    <TextBlock Text="1" TextWrapping="Wrap" Foreground="Blue" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="23,0,0,0"/>
                               </InlineUIContainer> 
                                 <InlineUIContainer>  
                                    <TextBlock Text=" Years" TextWrapping="Wrap"  Foreground="Blue"  HorizontalAlignment="Left" VerticalAlignment="Center"/>
                               </InlineUIContainer> 
                                   <Run Text=" "/> 
                                 <InlineUIContainer>  
                                    <TextBlock Text="Status:" TextWrapping="Wrap"   Foreground="Green"  HorizontalAlignment="Left" VerticalAlignment="Center"/>
                               </InlineUIContainer> 
                                    <Run Text=" "/> 
                                 <InlineUIContainer>  
                                    <TextBlock Text="Approved" TextWrapping="Wrap"  Foreground="Green"  HorizontalAlignment="Left" VerticalAlignment="Center"/>
                               </InlineUIContainer> 
                                    </TextBlock>
                        </Expander.Header>
                        <TextBlock Text="Testing1....." Background="#FFFFFFFF" Margin="0,4,0,0" TextWrapping="Wrap"/>
                    </Expander>
  </Grid>
</Page>