开发者

Making text in a Silverlight datagrid cell bold upon selection

开发者 https://www.devze.com 2023-03-28 21:14 出处:网络
I am trying to make a the text of a selected cell bold in Silverlight 4 using XAML. I have tried modifying the DataGridCell template, but I have come only as far 开发者_StackOverflow社区as modifying t

I am trying to make a the text of a selected cell bold in Silverlight 4 using XAML. I have tried modifying the DataGridCell template, but I have come only as far 开发者_StackOverflow社区as modifying the background color on selection and can't find anything for the font weight.


This is what I did and works for me:

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

The binding code in 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;

look at this code in the cell style:

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

精彩评论

暂无评论...
验证码 换一张
取 消