2013-02-07 39 views
1

我已經生成了一個Datavisualization折線圖,現在我正在嘗試構建一個自定義工具提示並且接近,但尚未完成。這是我對View的相關XAML(使用MVVM)。在數據可視化圖表系列中使用relativesource findancestor

<charting:Chart x:Name="Chart1" Height="Auto" Width="Auto" Title="Profit and Loss" 
      DockPanel.Dock="Bottom" 
      PlotAreaStyle="{StaticResource PlotAreaStyle}" 
      MinHeight="200" 
      MinWidth="200" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch"> 
    <charting:Chart.Resources> 
     <ControlTemplate x:Key="LineDataPointTemplate" TargetType="charting:LineDataPoint"> 
      <Grid x:Name="Root" Opacity="1"> 
       <ToolTipService.ToolTip> 
        <StackPanel> 
         <ContentControl> 
          <ContentControl.Content> 
           <MultiBinding Converter="{StaticResource MultiFormattingConverter}"> 
            <MultiBinding.Bindings> 
             <Binding Path="Title" > 
              <Binding.RelativeSource> 
               <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" /> 
              </Binding.RelativeSource> 
             </Binding> 
             <Binding RelativeSource="{RelativeSource TemplatedParent}"/> 
            </MultiBinding.Bindings> 
           </MultiBinding> 
          </ContentControl.Content> 
         </ContentControl> 
        </StackPanel> 
       </ToolTipService.ToolTip> 
      </Grid> 
     </ControlTemplate> 
    </charting:Chart.Resources> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Loaded"> 
      <ei:CallMethodAction TargetObject="{Binding}" MethodName="ChartLoadedMethod" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <charting:Chart.Series> 
     <charting:LineSeries x:Name="entrySeries" ItemsSource="{Binding EntryDataPointColl}" 
      Title="{Binding entryDateLegend}" 
      d:DataContext="EntryDataPointColl.xml" 
      IndependentValueBinding="{Binding Path=Xvalue}" 
      FlowDirection="LeftToRight" 
      DependentValueBinding="{Binding Path=Yvalue}" 
      > 
     <charting:LineSeries.DataPointStyle> 
      <Style TargetType="{x:Type charting:LineDataPoint}"> 
       <Setter Property="Visibility" Value="Collapsed" /> 
       <Setter Property="Opacity" Value="0" /> 
       <Setter Property="Background" Value="DarkGreen" /> 
       <Setter Property="Template" Value="{StaticResource LineDataPointTemplate}"/> 

嗯,我不知道如何輸入代碼...我縮進4個空格,然後做了一個粘貼,但似乎沒有奏效。

在任何情況下,我想用相對源來指的LineSeriesChart本身,以便在我MultiFormattingConverter,我將有機會獲得DataContext(我的虛擬機)和集合。然後我可以使用來自「點」的數據,它是TemplatedParent給我的數據,用於查找我所有3個LineSeries的值,並生成一個工具提示,其中包含每個可觀察集合的值。不管我在FindAncestor, AncestorType=代碼中指定的是什麼,我仍然以「unset」作爲值數組中的第一個對象。

上面的代碼是使用一個變化,通過指定我UserControl,但它不工作。我試過charting:Chartcharting:Chart1charting:LineSeries,charting:entrySeries但是沒有任何東西似乎適用於我。有人能幫我弄清楚在FindAncestor中指定什麼嗎?

+0

是的,他們在這裏使用文本區域古怪反應制表符代替空格 - 我總是粘貼任何我打算寫到記事本++和首先做一個標籤 - >空間轉換... – JerKimball

回答

0

這裏的問題是ToolTip結構不是Chart可視化樹的孩子,所以沒有辦法讓它在樹上向上/向下走,找到你正在尋找的東西 - 事實上,如果你想要看看這個與史努比或其他一些WPF嗅探工具,你會看到在該ToolTip樹有效地「結束」就在您的StackPanel(或非常接近)

嘗試使用ElementName綁定,而不是FindAncestor,並指定圖表/系列元素的名稱。有時候可以用來解決這類問題。

另一種方法是創建一種可以從該工具提示內部代理回Chart的「墊片」,但這有點超出了我現在願意鍵入手機的程度。 :)

+0

好吧,我不知道......因爲我所做的是在我的MultiFormattingConverter代碼中使用Mole2010,其中我在值[1](這是工具提示)上做了VisualTreeHelper.GetParent,父代是Canvas - 在這種情況下是PolyLine。並且,PolyLine(canvas)具有datacontext,我的viewmodel:ModelOptionChainViewModel。所以,在這一點上,我可以忘掉所有這些相關的源代碼,並簡單地使用TemplatedParent(LineDataPoint)使用GetParent(Canvas或PolyLine)並提取datacontext,其中我的3個可觀察集合... – JohnBlacker

+0

糾正, TemplatedParent是LineDataPoint,而不是我以前發佈的工具提示...對不起。 – JohnBlacker

+0

@JohnBlacker呃......你是說你錯過了原始問題,或者你改變了XAML的結構,以至於你不再需要在'ToolTip'中進行綁定? – JerKimball

0

我沒有嘗試上面建議的ElementName方法。相反,我決定通過使用正常的FormattingConverter來簡化這個值,通過的值是TemplatedParent - LineDataPoint。現在我使用GetParent(ldp),並且我有Canvas,它有我的vm,因爲它是datacontext。在這一點上,我可以訪問我的可觀察集合,並可以提取我想要包含在我的自定義工具提示中的值。 感謝所有海報。

相關問題