开发者

Transfer piece of xaml to resource

I have 8 columns and every of them has the same datacontext.

<!-- COLUMN: PREVIEW MESSAGE -->
                    <data:DataGridTemplateColumn x:Name="PreviewColumn" CanUserSort="True"
                                             SortMemberPath="Preview" Width="*">
                        <data:DataGridTemplateColumn.CellTemplate>

                            <DataTemplate>
                                <TextBlock Text="{Binding Preview}" 
                                       FontWeight="{开发者_运维问答Binding IsBold, Converter={StaticResource cnvFontWeight}}"
                                       Foreground="{Binding IsOverdueMessage, Converter={StaticResource cnvOverdue}}"
                                       VerticalAlignment="Center"
                                       Margin="5,0,5,0">
                                <telerik:RadContextMenu.ContextMenu>
                                    <telerik:RadContextMenu Opened="inboxContextMenu_Opened" ItemClick="inboxContextMenu_ItemClick">
                                        <telerik:RadMenuItem Header="Forward message" Loaded="ForwardMessageMenuItem_Loaded"/>
                                                                                                                    </telerik:RadContextMenu>
                                </telerik:RadContextMenu.ContextMenu>
                                </TextBlock>
                            </DataTemplate>

                        </data:DataGridTemplateColumn.CellTemplate>

                    </data:DataGridTemplateColumn>

What is the most practical way to make this contextmenu reusable? It is in every column identical. I haven't much experience in silverlight. I'm using silverlight 4.


First I thought, you could create a specific style for the TextBlock, and then put the ContextMenu inside the style. However, because TextBlock is not inherited from ContentControl, you can't simply do it.

What you then can do is, use a Label instead of the TextBlock. After including the sdk namespace, you would have something like this,

xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"

    <sdk:Label Content="{Binding Preview}" 
               FontWeight="{Binding IsBold, Converter={StaticResource cnvFontWeight}}" 
               Foreground="{Binding IsOverdueMessage, Converter={StaticResource cnvOverdue}}" 
               VerticalAlignment="Center" 
               Margin="5,0,5,0"
               Style="{StaticResource LabelWithContextMenuStyle}"/>

You can see I have specified a style for this Label control.

This style basically is the Label's default style with an additional ContextMenu sitting inside its ContentControl.

    <Style x:Key="LabelWithContextMenuStyle" TargetType="sdk:Label">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="sdk:Label">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="Valid"/>
                                <VisualState x:Name="Invalid">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0:0:1.5" Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentControl">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="Red"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="RequiredStates">
                                <VisualState x:Name="NotRequired"/>
                                <VisualState x:Name="Required">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="FontWeight" Storyboard.TargetName="ContentControl">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="SemiBold"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="2" Padding="{TemplateBinding Padding}">
                            <ContentControl x:Name="ContentControl" Cursor="{TemplateBinding Cursor}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsTabStop="False" VerticalAlignment="{TemplateBinding VerticalAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Margin="131,106,0,0">
                                <telerik:RadContextMenu.ContextMenu> 
                                    <telerik:RadContextMenu Opened="RadContextMenu_Opened" ItemClick="RadContextMenu_ItemClick"> 
                                        <telerik:RadMenuItem Loaded="RadMenuItem_Loaded"/> 
                                    </telerik:RadContextMenu> 
                                </telerik:RadContextMenu.ContextMenu> 
                            </ContentControl>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

This is it. I hope this helps. :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜