选择后,使Silverlight数据网格单元格中的文本变为粗体

时间:2011-08-17 10:07:02

标签: silverlight templates xaml datagrid cell

我正在尝试使用XAML在Silverlight 4中创建所选单元格的文本。我已经尝试修改DataGridCell模板,但我只是在选择时修改背景颜色,并且找不到任何字体粗细。

1 个答案:

答案 0 :(得分:1)

这就是我所做的并且对我有用:

<UserControl.Resources>
        <Style x:Key="DataGridCellStyle1" TargetType="sdk:DataGridCell">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="sdk:DataGridCell">
                        <Grid x:Name="Root" Background="{TemplateBinding Background}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CurrentStates">
                                    <VisualState x:Name="Regular"/>
                                    <VisualState x:Name="Current">
                                        <Storyboard>
                                            <DoubleAnimation  Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisual"/>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter"
                                                                           Storyboard.TargetProperty="(Content).FontWeight"
                                                                           Duration="0">
                                                <DiscreteObjectKeyFrame KeyTime="0"
                                                                        Value="Bold" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="ValidationStates">
                                    <VisualState x:Name="Valid"/>
                                    <VisualState x:Name="Invalid">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="InvalidVisualElement"/>
                                            <ColorAnimation Duration="0" To="#FFFFFFFF" Storyboard.TargetProperty="(Fill).Color" Storyboard.TargetName="FocusVisual"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Rectangle x:Name="FocusVisual" Fill="#66FFFFFF" HorizontalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" Stroke="Blue" StrokeThickness="3" VerticalAlignment="Stretch"/>
                            <ContentPresenter x:Name="contentPresenter"
                                              Margin="0"                                              
                                              FlowDirection="RightToLeft"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              Content="{TemplateBinding Content}"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            </ContentPresenter>
                                <Rectangle x:Name="InvalidVisualElement" HorizontalAlignment="Stretch" IsHitTestVisible="False" Opacity="0" Stroke="#FFDC000C" StrokeThickness="1" VerticalAlignment="Stretch"/>
                            <Rectangle x:Name="RightGridLine" Grid.Column="1" VerticalAlignment="Stretch" Width="1"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">        
        <sdk:DataGrid AutoGenerateColumns="False"
                      Height="154"
                      HorizontalAlignment="Left"
                      Margin="38,41,0,0"
                      Name="dataGrid1"
                      VerticalAlignment="Top"
                      Width="182" CellStyle="{StaticResource DataGridCellStyle1}"   >
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Binding="{Binding Key}"  />
                <sdk:DataGridTextColumn Binding="{Binding Value}" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>         
    </Grid>

MainPage.cs中的绑定代码

    IDictionary<string, object> test = new Dictionary<string, object>();
    test.Add(new KeyValuePair<string, object>("Name", "A"));
    test.Add(new KeyValuePair<string, object>("Height", 2));

    this.dataGrid1.ItemsSource = test;

以单元格样式查看此代码:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter"
                               Storyboard.TargetProperty="(Content).FontWeight"
                               Duration="0">
    <DiscreteObjectKeyFrame KeyTime="0"
                            Value="Bold" />
</ObjectAnimationUsingKeyFrames>