如何从wpf后面的代码更改数据网格标题的字体大小

时间:2014-05-15 12:06:05

标签: c# wpf silverlight

如何更改标题的样式(标题字体,动态后面的代码系列)。

 <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>

1 个答案:

答案 0 :(得分:0)

您可以使用动态资源来实现:1)声明资源
2)在属性中使用它(作为动态资源,而不是静态)
3)在后面的代码中更改资源< / p>

<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。最好使用MVVM。