2011-09-27 79 views
2

我有8列,每個列都有相同的數據上下文。將xaml傳輸到資源

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

是什麼使這個文本菜單可重複使用的最實用的方法是什麼?它在每一列都是相同的。我沒有太多的Silverlight經驗。我使用silverlight 4.

+0

您的整個DataTemplate是相同還是隻有ContextMenu? – Mart

+0

只有上下文菜單。每列都有不同的綁定。 – user278618

+0

你好,你忘了這個問題嗎? –

回答

1

首先我想,你可以爲TextBlock創建一個特定的樣式,然後將ContextMenu放入樣式中。但是,因爲TextBlock不是從ContentControl繼承的,所以不能簡單地這樣做。

然後你可以做的是,使用Label而不是TextBlock。包括SDK命名空間後,你將有這樣的事情,

的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}"/> 

您可以看到我爲此Label控件指定了一種樣式。

此樣式基本上是Label的默認樣式,其內部的ContextMenu坐落在其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> 

這就是它。我希望這有幫助。 :)