ExpanderのHeaderをStretchさせる

##Problem
ExpanderコントロールのHeader, HeaderTemplateに書いた中身はStretchされません。

```NotStretched.xaml
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
    <Expander Margin="5">
        <Expander.Header>
            <Grid Background="PaleGreen">
                <TextBox Text="Not stretched." BorderBrush="Green" />
            </Grid>
        </Expander.Header>
    </Expander>
</Border>
```

![Screen Shot 2014-04-18 at 14.31.46.png](https://qiita-image-store.s3.amazonaws.com/0/34563/969a4c04-51af-280c-cbdc-3d43e3958145.png "Screen Shot 2014-04-18 at 14.31.46.png")


なかなか解決策を思いつけなかったのでメモしておきます。
コードビハインドやビヘイビアを使わずにXAMLだけで解決したいところです。

##Solution?
ActualWidthをバインドする方法。

```ActualWidth.xaml
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
    <Expander Margin="5">
        <Expander.Header>
            <Grid Background="GreenYellow"
              Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}">
                <TextBox Text="Stretched?" BorderBrush="Indigo" />
            </Grid>
        </Expander.Header>
    </Expander>
</Border>
```
![Screen Shot 2014-04-18 at 14.31.53.png](https://qiita-image-store.s3.amazonaws.com/0/34563/77444a9b-a983-d8bf-3286-42afebaf4196.png "Screen Shot 2014-04-18 at 14.31.53.png")

違う気がします。

##Solution
HorizontalAlignmentをOneWayToSourceバインドする方法。

```HorizontalAlignment.xaml
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
    <Expander Margin="5">
        <Expander.Header>
            <Grid HorizontalAlignment="{Binding HorizontalAlignment, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}">
                <TextBox Text="Stretched." BorderBrush="DeepPink" />
            </Grid>
        </Expander.Header>
    </Expander>
</Border>
```
![Screen Shot 2014-04-18 at 14.31.58.png](https://qiita-image-store.s3.amazonaws.com/0/34563/3c4eee5e-5ce5-01d1-cfd8-6de691ff2cc0.png "Screen Shot 2014-04-18 at 14.31.58.png")

欲しいのはこれでした。

##Conclusion
要はHeaderのContentPresenterのHorizontalAlignmentがLeftで強制されているのが原因です。これに適切な値を入れることで回避できます。

参考にしたのはこちら。
[Josh Smith on WPF](http://joshsmithonwpf.wordpress.com/2007/02/24/stretching-content-in-an-expander-header/)

##Sample
![Screen Shot 2014-04-18 at 14.31.29.png](https://qiita-image-store.s3.amazonaws.com/0/34563/7d060ac3-4e0f-62ee-fb62-456919c59ec1.png "Screen Shot 2014-04-18 at 14.31.29.png")

```ExpanderTest.xaml
<Window x:Class="ExpanderTest.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="200" Width="525">    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition />            <ColumnDefinition />            <ColumnDefinition />        </Grid.ColumnDefinitions>        <Border Grid.Column="0" BorderBrush="Black" BorderThickness="1" Margin="2">            <Expander Margin="5">                <Expander.Header>                    <Grid Background="PaleGreen">                        <TextBox Text="Not stretched." BorderBrush="Green" />                    </Grid>                </Expander.Header>            </Expander>        </Border>        <Border Grid.Column="1" BorderBrush="Black" BorderThickness="1" Margin="2">            <Expander Margin="5">                <Expander.Header>                    <Grid Background="GreenYellow"                      Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}">                        <TextBox Text="Stretched?" BorderBrush="Indigo" BorderThickness="1" />                    </Grid>                </Expander.Header>            </Expander>        </Border>        <Border Grid.Column="2" BorderBrush="Black" BorderThickness="1" Margin="2">            <Expander Margin="5">                <Expander.Header>                    <Grid HorizontalAlignment="{Binding HorizontalAlignment, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}">                        <TextBox Text="Stretched." BorderBrush="DeepPink" />                    </Grid>                </Expander.Header>            </Expander>        </Border>    </Grid></Window>
```