LoginSignup
0
4

More than 3 years have passed since last update.

WPF-DataGridのヘッダーへのバインドはちょっと仕掛けが必要

Last updated at Posted at 2021-05-06

DataGridは要素の宣言のところにアイテムソースを記述する。
ヘッダーをバインドしたい先がDataGridのアイテムソースの下にいない場合、DataContextからのパスを指定しないといけない。
しかし、なぜか普通にはDataContextまで辿れない。この議論は下記サイトにあるが、結論としてDataContextを参照する為のプロキシを用意してリソースディクショナリに入れておかないといけない。
https://stackoverflow.com/questions/7711275/bind-datagrid-column-visibility-mvvm

        <Grid.Resources>
            <FrameworkElement x:Key="proxyElement" />
        </Grid.Resources>
        <ContentControl Visibility="Collapsed" Content="{StaticResource proxyElement}" />
        <DataGrid ItemsSource="{Binding DataList}" AutoGenerateColumns="False">
            ~ 省略 ~
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="{Binding Path=DataContext.Data.Value, Source={StaticResource proxyElement}}">
            ~ 省略 ~
            </DataGrid.Columns>
        </DataGrid>


0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4