wpf

2012-11-16 29 views
8

में पदानुक्रम डेटा टेम्पलेट में वृक्षदृश्य आइटम के लिए संदर्भ मेनू कैसे प्रदर्शित करें पदानुक्रम डेटा टेम्पलेट का उपयोग करके wpf में वृक्ष दृश्य आइटम के संदर्भ मेनू को कैसे प्रदर्शित करें? कैसे केवल CountryTemplate के लिए संदर्भ मेनू प्रदर्शित करने के लिए:wpf

<HierarchicalDataTemplate x:Key="DispTemplate"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Path=Label}" Style="{StaticResource TreeTextStyle}" ToolTip="{Binding Path=Description}" Tag="{Binding Path=Tag}"> 
      </TextBlock> 
     </StackPanel> 
    </HierarchicalDataTemplate> 
    <HierarchicalDataTemplate x:Key="BuildingTemplate" ItemsSource="{Binding Path=Building}" ItemTemplate="{StaticResource BuildingTemplate}"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Path=Label}" Style="{StaticResource TreeTextStyle}" ToolTip="{Binding Path=Description}"/> 
     </StackPanel> 
    </HierarchicalDataTemplate> 
    <HierarchicalDataTemplate x:Key="CityTemplate" ItemsSource="{Binding Path=City}" ItemTemplate="{StaticResource CityTemplate}"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Path=Label}" Style="{StaticResource TreeTextStyle}" ToolTip="{Binding Path=Description}"/> 
     </StackPanel> 
    </HierarchicalDataTemplate> 
    <HierarchicalDataTemplate x:Key="CountryTemplate" ItemsSource="{Binding Path=Country}" ItemTemplate="{StaticResource CountryTemplate}"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Path=RootName}" Style="{StaticResource TreeTextStyle}" ToolTip="{Binding Path=Description}"/> 
     </StackPanel> 
    </HierarchicalDataTemplate> 

उत्तर

11

तुम भी जोड़ सकते हैं ContextMenu की Itemcontainerstyle में किसी भी दृश्य बच्चे के लिए जोड़ सकते हैं डेटा टेम्पलेट, उदाहरण के लिए:

<HierarchicalDataTemplate x:Key="CountryTemplate" ItemsSource="{Binding Path=Country}" ItemTemplate="{StaticResource CountryTemplate}"> 
    <StackPanel Orientation="Horizontal"> 
     <StackPanel.ContextMenu> 
      <ContextMenu> 
       <MenuItem Header="Header" Command="{Binding Command}"/> <!--This command should be in the data context (each country item)--> 
      </ContextMenu> 
     </StackPanel.ContextMenu> 
     <TextBlock Text="{Binding Path=RootName}" Style="{StaticResource TreeTextStyle}" ToolTip="{Binding Path=Description}"/> 
    </StackPanel> 
</HierarchicalDataTemplate> 
+0

मैं व्यूमोडेल की जड़ में परिभाषित कमांड से कैसे जुड़ूं। अभी तक FindAncestor विधि काम नहीं कर रही है। क्या आप कृपया मुझे इसका उदाहरण दिखा सकते हैं? – TrustyCoder

+0

उन मामलों में जहां मैं बाध्यकारी बनाने के लिए दृश्य मॉडल में आसानी से नहीं पहुंच सकता, मैं इस खोजकर्ता के साथ प्रयास करने की कोशिश करता हूं: आदेश = "{बाइंडिंग डेटाकॉन्टेक्स्ट.कॉमैंड, सापेक्ष स्रोत = {सापेक्ष स्रोत ढूँढें पूर्वजों, पूर्वज प्रकार = {x: टाइप मेनविंडो}}} ", आम तौर पर मुख्य विंडो के डेटाकॉन्टेक्स्ट में रूट व्यू मॉडल होता है, और वही आदेश होता है जो आप चाहते हैं। –

+0

यह किसी कारण से संदर्भ मेनू के साथ काम नहीं करता है क्योंकि यह दृश्य पेड़ का हिस्सा नहीं है। – TrustyCoder

3
<HierarchicalDataTemplate x:Key="CountryTemplate" ItemsSource="{Binding Path=Country}" ItemContainerStyle="{StaticResource CountryTemplateItemContainerStyle}" ItemTemplate="{StaticResource CountryTemplate}"> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Text="{Binding Path=RootName}" Style="{StaticResource TreeTextStyle}" ToolTip="{Binding Path=Description}" /> 
         </StackPanel> 
</HierarchicalDataTemplate> 


<Style x:Key="CountryTemplateItemContainerStyle" TargetType="{x:Type TreeViewItem}"> 
<Setter Property="ContextMenu" Value="{DynamicResource TreeViewContextMenu}"/> 
</Style> 
<ContextMenu x:Key="TreeViewContextMenu"> 
     <MenuItem .../> 
</ContextMenu> 

आप देख सकते हैं, तो आप अपने ContextMenu HierarchicalDataTemplate

+0

यह मेरे लिए काम नहीं किया। – TrustyCoder

+0

यह मेरे लिए बहुत अच्छा काम करता है, और, मुझे यह पसंद है कि ContextMenu को आइटम के करीब कैसे परिभाषित किया गया है। –

+0

यह मेरे लिए काम किया। संकलन त्रुटि से बचने के लिए, मुझे चारों ओर ब्लॉक को स्वैप करना था (सुनिश्चित करें कि XAML टैग 'श्रेणीबद्ध डेटाटाम्प्लेट' 'स्टाइल' और' संदर्भ मेनू 'टैग के बाद आते हैं)। कारण यह काम करता है क्योंकि ContextMenu को शैली में स्थानांतरित करने से सबकुछ एक अधिक निकट से संबंधित दृश्य पेड़ में बदल जाता है, जिसका अर्थ है कि डेटाकॉन्टेक्स्ट बिना किसी परेशानी के पाया जा सकता है। – Contango

3

मूल रूप से मैं इस

के साथ आया था
<HierarchicalDataTemplate x:Key="ChildTemplate"> 
      <StackPanel Orientation="Horizontal"> 
       <StackPanel.ContextMenu> 
        <ContextMenu> 
         <MenuItem Header="Copy" CommandParameter="{Binding CopyTag}"> 
         </MenuItem> 
         <MenuItem Header="Paste" CommandParameter="{Binding PasteTag}"> 
         </MenuItem> 
         <ContextMenu.ItemContainerStyle> 
          <Style TargetType="MenuItem"> 
           <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.CopyPaste}"/> 
          </Style> 
         </ContextMenu.ItemContainerStyle> 
        </ContextMenu> 
       </StackPanel.ContextMenu> 
       <Image Source="/Images/Child.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource TreeIconStyle}"/> 
       <TextBlock Text="{Binding Path=Label}" Style="{StaticResource TreeTextStyle}" ToolTip="{Binding Path=Description}" Tag="{Binding Path=Tag}"> 
       </TextBlock> 
      </StackPanel> 
     </HierarchicalDataTemplate> 

और कॉपी और पेस्ट के लिए एक ही कमांड में कॉपी और पेस्ट करने के लिए अलग-अलग पैरामीटर हैं।

2

संदर्भ मेनू मेनू के रूप में स्पष्ट रूप से काम नहीं करते हैं, क्योंकि डिफ़ॉल्ट रूप से, वे अन्य सभी चीज़ों के लिए एक अलग दृश्य पेड़ में हैं, इसलिए DataContext नहीं मिल सकता है।

कुंजी अंतर्दृष्टि एक <Style> कि एक संदर्भ मेनू को परिभाषित करता है बनाने के लिए, फिर एक लक्ष्य तत्व है, जो संदर्भ मेनू हुक करने के लिए है कि शैली देते है। यह संदर्भ मेनू को एक दृश्य पेड़ में बदल देता है जो डिफ़ॉल्ट DataContext के साथ रेखांकित है।

पहले, शैली बनाने:, तब

<UserControl.Resources>                               
    <ResourceDictionary> 

     <!-- For the context menu to work, we must shift it into a style, which means that the context menu is now in a 
     visual tree that is more closely related to the current data context. All we have to do then is set the style, 
     which hooks up the context menu. --> 
     <Style x:Key="ContextMenuStyle" TargetType="{x:Type StackPanel}"> 
      <Setter Property="ContextMenu" Value="{DynamicResource TreeViewContextMenu}"/> 
     </Style> 
     <ContextMenu x:Key="TreeViewContextMenu"> 
      <MenuItem Header="Test" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.CmdDisplayDetailsGraph}"/> 
     </ContextMenu> 

संदर्भ मेनू हुक अप कहीं भी आप चाहते हैं, अलग-अलग दृश्य पेड़ों की वजह से समस्याओं में घिर गए बिना।

उदाहरण 1:

<HierarchicalDataTemplate DataType="{x:Type snapshot:Details}" ItemsSource="{Binding DetailsList}"> 
    <StackPanel Orientation="Vertical" Style="{StaticResource ContextMenuStyle}"> 
     <ContentPresenter Content="{Binding}" ContentTemplate="{Binding View.DefaultDataRowTemplate}" /> 
</StackPanel> 

उदाहरण 2:

<DataTemplate DataType="{x:Type snapshot:InstrumentDetails}"> 
    <StackPanel Orientation="Vertical" Style="{StaticResource ContextMenuStyle}">     
     <Grid HorizontalAlignment="Stretch" VerticalAlignment="Center">