Question

How do i change the style of a header(header font , family from code behind dynamically).

 <Grid Height="500" Width="710"  ManipulationBoundaryFeedback="Grid_ManipulationBoundaryFeedback" FocusVisualStyle="{StaticResource MyFocusVisualStyle}">

        <DataGrid  FocusVisualStyle="{StaticResource MyFocusVisualStyle}" BorderBrush="Transparent" BorderThickness="0" x:Name="testgrid" Height="450" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalScrollBarVisibility="Disabled" CanUserResizeColumns="False" CanUserResizeRows="False"  CanUserReorderColumns="False"
     Background="#ffffff" MaxWidth="710" GridLinesVisibility="None" AutoGenerateColumns="False" Margin="0,0,0,0" CanUserAddRows="false" HeadersVisibility="Column" LayoutUpdated="testgrid_LayoutUpdated">

            <DataGrid.Columns>
                <DataGridTemplateColumn x:Name="firstColumn" Header="File Name" Width="337">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox Margin="0,0,10,0" IsChecked="{Binding Path=IsSelected,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}"  Style="{StaticResource CheckBoxStyle2}"></CheckBox>
                                <TextBlock Text="{Binding Path=FileName}"  ToolTip="{Binding Path=FileName}" TextTrimming="CharacterEllipsis" Width="290" Style="{StaticResource TextBlockContentDark}"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Height" Value="40" />
                            <Setter Property="Background" Value="#ffffff" />
                            <Setter Property="BorderBrush" Value="#001932" />
                            <Setter Property="BorderThickness" Value="0,0,0,1" />
                            <Setter Property="FontFamily" Value="pack://application:,,,/fonts/#xxx Clear"></Setter>
                            <Setter Property="Foreground" Value="#001932"/>
                            <Setter Property="FontSize" Value="12"/>
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="HorizontalAlignment" Value="Left"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                            <Setter Property="Height" Value="30"/>
                            <Setter Property="Background" Value="#ffffff"/>
                            <Setter Property="BorderBrush" Value="Transparent"/>
                            <Setter Property="BorderThickness" Value="0"/>
                            <Setter Property="Margin" Value="10,0,0,0"/>
                            <Setter Property="FontFamily" Value="pack://application:,,,/fonts/#xxx Clear"></Setter>
                            <Setter Property="Foreground" Value="#001932"/>
                            <Setter Property="FontSize" Value="16"/>
                            <Setter Property="Focusable" Value="False"/>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="File Size" Width="120">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=FileSizeDisplay}" Style="{StaticResource TextBlockContentDark}"></TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Height" Value="40" />
                            <Setter Property="Background" Value="#ffffff" />
                            <Setter Property="BorderBrush" Value="#001932" />
                            <Setter Property="FontFamily" Value="pack://application:,,,/fonts/#xxx Clear"></Setter>
                            <Setter Property="Foreground" Value="#001932"/>
                            <Setter Property="FontSize" Value="16"/>
                            <Setter Property="BorderThickness" Value="1,0,0,1" />
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="HorizontalAlignment" Value="Center"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                            <Setter Property="Height" Value="30"/>
                            <Setter Property="Margin" Value="0,0,0,0"/>
                            <Setter Property="Background" Value="#ffffff"/>
                            <Setter Property="BorderBrush" Value="Transparent"/>
                            <Setter Property="BorderThickness" Value="0"/>
                            <Setter Property="FontFamily" Value="pack://application:,,,/fonts/#xxx Clear"></Setter>
                            <Setter Property="Foreground" Value="#001932"/>
                            <Setter Property="FontSize" Value="16"/>
                            <Setter Property="Focusable" Value="False"/>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Date Modified" Width="120">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=FileModifiedDate}" Style="{StaticResource TextBlockContentDark}"></TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Height" Value="40" />
                            <Setter Property="Background" Value="#ffffff" />
                            <Setter Property="BorderBrush" Value="#001932" />
                            <Setter Property="FontFamily" Value="pack://application:,,,/fonts/#xxx Clear"></Setter>
                            <Setter Property="Foreground" Value="#001932"/>
                            <Setter Property="FontSize" Value="16"/>
                            <Setter Property="BorderThickness" Value="1,0,0,1" />
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="HorizontalAlignment" Value="Center"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                            <Setter Property="Height" Value="30"/>
                            <Setter Property="Margin" Value="0,0,0,0"/>
                            <Setter Property="Background" Value="#ffffff"/>
                            <Setter Property="BorderBrush" Value="Transparent"/>
                            <Setter Property="BorderThickness" Value="0"/>
                            <Setter Property="FontFamily" Value="pack://application:,,,/fonts/#xxx Clear"></Setter>
                            <Setter Property="Foreground" Value="#001932"/>
                            <Setter Property="FontSize" Value="16"/>
                            <Setter Property="Focusable" Value="False"/>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Date Created" Width="121">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=FileModifiedDate}"></TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                            <Setter Property="Height" Value="40" />
                            <Setter Property="Background" Value="#ffffff" />
                            <Setter Property="BorderBrush" Value="#001932" />
                            <Setter Property="FontFamily" Value="pack://application:,,,/fonts/#xxx Clear"></Setter>
                            <Setter Property="FontSize" Value="16"></Setter>
                            <Setter Property="Foreground" Value="#001932"></Setter>
                            <Setter Property="BorderThickness" Value="1,0,0,1" />
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                    <DataGridTemplateColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="HorizontalAlignment" Value="Center"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                            <Setter Property="Height" Value="30"/>
                            <Setter Property="Margin" Value="0,0,0,0"/>
                            <Setter Property="Background" Value="#ffffff"/>
                            <Setter Property="BorderBrush" Value="Transparent"/>
                            <Setter Property="BorderThickness" Value="0"/>
                            <Setter Property="FontFamily" Value="pack://application:,,,/fonts/#xxx Clear Light"></Setter>
                            <Setter Property="Foreground" Value="#001932"/>
                            <Setter Property="FontSize" Value="16"/>
                            <Setter Property="Focusable" Value="False"/>
                        </Style>
                    </DataGridTemplateColumn.CellStyle>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Separator BorderThickness="5" BorderBrush="#b2bac1" VerticalAlignment="Bottom" Margin="0,0,0,50"/>
        <StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Left" Height="50" Orientation="Horizontal" Width="698">
            <Button  FocusVisualStyle="{StaticResource MyFocusVisualStyle}" x:Name="SelectAll" Style="{StaticResource CustomButton2}"  Click="SelectAll_Click"  Content="{x:Static res:Strings.SelectAll}" Margin="20,10,10,10" Padding="30,0,30,0" FontFamily="pack://application:,,,/fonts/#xxx Clear"  ></Button>
            <Button  FocusVisualStyle="{StaticResource MyFocusVisualStyle}" x:Name="SelectNone" Click="SelectNone_Click"  Content="{x:Static res:Strings.SelectNone}" Margin="20,10,10,10" Padding="30,0,30,0" Style="{StaticResource CustomButton2}" FontFamily="pack://application:,,,/fonts/#xxx Clear"></Button>
            <TextBlock Text="{x:Static res:Strings.FilesMigrated}" Margin="45,0,0,0" Style="{StaticResource TextBlockHeaderLightDark}" TextAlignment="Center" VerticalAlignment="Center"></TextBlock>
            <TextBlock Text="X" Margin="45,0,0,0" Style="{StaticResource TextBlockX}" TextAlignment="Center" VerticalAlignment="Center" PreviewMouseDown="Close_Click"></TextBlock>
        </StackPanel>


    </Grid>

No correct solution

OTHER TIPS

You can achieve it by using dynamic resource:
1) Declare resource
2) Use it in property (as dynamic resource, not static)
3) Change resource in code behind

<Grid x:Name="myGrid">
    <Grid.Resources>
        <system:Double x:Key="myFontSize">50</system:Double>
        <FontFamily x:Key="myFontFamily">Arial</FontFamily>
    </Grid.Resources>

    <TextBlock FontFamily="{DynamicResource myFontFamily}" FontSize="{DynamicResource myFontSize}">Hello World</TextBlock>

    <Button Height="50" Click="Button_Click">Change font</Button>
</Grid>

private void Button_Click(object sender, RoutedEventArgs e)
{
    myGrid.Resources["myFontSize"] = (double)99;
    myGrid.Resources["myFontFamily"] = new FontFamily("Verdana");
}

P.S. It is better to use MVVM.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top