[WPF] GridLength に StaticResource の値を設定する

2022-07-08 (金)

GridRowDefinition.Height, ColumnDefinition.Width に XAML 上で StaticResources で定義した値をバインドする方法です。

環境

  • .NET 6.0.301
  • C# 10.0
  • Visual Studio 2022 Version 17.2.5
  • Windows 10 Pro 64bit 21H1 19043.1766

実装

GridLength でバインドする例

<Grid>
    <Grid.Resources>
        <GridLength x:Key="GridLengthKey">10</GridLength>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="{StaticResource GridLengthKey}" />
    </Grid.RowDefinitions>
</Grid>

double でバインドする例

<Grid>
    <Grid.Resources>
        <system:Double x:Key="DoubleKey">10</system:Double>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding Source={StaticResource DoubleKey}}" />
    </Grid.RowDefinitions>
</Grid>

感謝

2022-07-08 (金)