0
我想使用XAML在Silverlight 4中使所選單元格粗體文本。我已經嘗試修改DataGridCell模板,但是我只能在選擇時修改背景顏色,並且找不到字體粗細的任何內容。使Silverlight 4中的文本在選擇時加粗
我想使用XAML在Silverlight 4中使所選單元格粗體文本。我已經嘗試修改DataGridCell模板,但是我只能在選擇時修改背景顏色,並且找不到字體粗細的任何內容。使Silverlight 4中的文本在選擇時加粗
這是我做過什麼,併爲我工作:
<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>
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;
看看這個代碼在單元格樣式中MainPage.cs綁定代碼:
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter"
Storyboard.TargetProperty="(Content).FontWeight"
Duration="0">
<DiscreteObjectKeyFrame KeyTime="0"
Value="Bold" />
</ObjectAnimationUsingKeyFrames>