2011-12-27 24 views
1

私たちのアプリケーションでは、XAMLは使用しませんが、C#のコードがありません。XAMLをC#コードに変換する際に 'ToolTip'に論理的または視覚的な親エラーがありません

ここ
@"<Style TargetType='charting:LineDataPoint'> 
    <Setter Property='Background' Value='Blue'/> 
    <Setter Property='Width' Value='7' /> 
    <Setter Property='Height' Value='7' /> 
    <Setter Property='Template'> 
     <Setter.Value> 
     <ControlTemplate TargetType='charting:LineDataPoint'> 
      <Grid Opacity='1'> 
      <ToolTipService.ToolTip> 
       <StackPanel Margin='2,2,2,2'> 
       <ContentControl Content='{Binding Path=Key}' ContentStringFormat='" + "Date" + @": {0:d}'/> 
       <ContentControl Content='{Binding Path=Value}' ContentStringFormat='" + "Max." + @": {0:n}'/> 
       </StackPanel> 
      </ToolTipService.ToolTip> 
      <Ellipse Opacity='1' StrokeThickness='1' Stroke='#FF686868' Fill='{TemplateBinding Background}'/> 
     </Grid> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style>"; 

私が試してみましたが、私はそれを実行したとき、私はエラーメッセージを取得「『ヒント』は論理的または視覚的な親を持つことができません。」コード

var styleDataPoint = new Style(typeof(LineDataPoint)); 
var backGroundSetter = new Setter { Property = BackgroundProperty, Value = Brushes.Blue }; 
var widthSetter = new Setter { Property = WidthProperty, Value = 10.0 }; 
var heightSetter = new Setter { Property = HeightProperty, Value = 10.0 }; 
var dataPointTemplate = new Setter { Property = TemplateProperty }; 
var template = new ControlTemplate(typeof(LineDataPoint)); 

var gridElementFactory = new FrameworkElementFactory(typeof(Grid)); 
gridElementFactory.SetValue(NameProperty, "Root"); 
gridElementFactory.SetValue(OpacityProperty, 1.0); 

var toolTiplElementFactory = new FrameworkElementFactory(typeof(ToolTip)); 

var stackPanelElementFactory = new FrameworkElementFactory(typeof(StackPanel)); 
stackPanelElementFactory.SetValue(MarginProperty, new Thickness(2, 2, 2, 2)); 
var tooltipLine = new FrameworkElementFactory(typeof(ContentControl)); 
tooltipLine.SetValue(ContentStringFormatProperty, "Min." + @": {0:d}"); 
tooltipLine.SetBinding(ContentProperty, new Binding("Key")); 
stackPanelElementFactory.AppendChild(tooltipLine); 

var tooltipLine1 = new FrameworkElementFactory(typeof(ContentControl)); 
tooltipLine1.SetValue(ContentStringFormatProperty, "Max." + @": {0:n}"); 
tooltipLine1.SetBinding(ContentProperty, new Binding("Value")); 
stackPanelElementFactory.AppendChild(tooltipLine1); 

toolTiplElementFactory.AppendChild(stackPanelElementFactory); 

var ellipselElementFactory = new FrameworkElementFactory(typeof(Ellipse)); 
ellipselElementFactory.SetValue(OpacityProperty, 1.0); 
ellipselElementFactory.SetValue(Shape.StrokeThicknessProperty, 1.0); 
ellipselElementFactory.SetValue(Shape.FillProperty, Brushes.Blue); 

gridElementFactory.AppendChild(toolTiplElementFactory); 
gridElementFactory.AppendChild(ellipselElementFactory); 

template.VisualTree = gridElementFactory; 
dataPointTemplate.Value = template; 

styleDataPoint.Setters.Add(backGroundSetter); 
styleDataPoint.Setters.Add(widthSetter); 
styleDataPoint.Setters.Add(heightSetter); 
styleDataPoint.Setters.Add(dataPointTemplate); 
+5

なぜXAMLを使用しないのですか? – SLaks

+0

あなたのアプリケーションはWPFアプリケーションのように見えますが、この質問に対する簡単な答えは、実際にXAMLを使用しています。その他の情報が必要な場合は、この質問にそのまま回答することはできません。 –

+0

スタックパネルのようなものを使うことで、あなたは完全にWPFを使用しています。この作業を行い、まだXAMLを使用しないことは非常に難しいでしょう。 IMOでは、WPFアプリケーションでXAMLを使用しない本当の理由を思いつきます。詳しく教える? – McKay

答えて

0

それは通常のDOMあったように自分でビジュアルツリーを構築するべきではないこと(=実現)私はずっと前も似たような直面した、と私は読んで。代わりにテンプレート(テンプレートを作成するためにコード(C#など)を使用するテンプレートを使用し、それを適用する必要があります。

つまり、廃止予定のAPIがあるにもかかわらず、FrameworkElementFactoryに従ってください。私は個人的に何かに頼るのが嫌なのが好きではありませんが、必要なものを達成するための最良の方法と思われます。

しかし、「非常に良い」方法はXAMLを使用しています!

乾杯

関連する問題