& # 39;提示# 39;在将XAML转换为c#代码时,不能有逻辑或视觉上的父错误

本文关键字:错误 不能 视觉上 代码 XAML 在将 提示 转换 | 更新日期: 2023-09-27 18:18:49

在我们的应用程序中,我们不使用XAML,但是c#代码后面,所以请帮我把这个XAML代码转换成c#。

@"<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>";
这里是我尝试过的代码,但是当我运行它时,我得到错误消息"'ToolTip'不能有逻辑或可视化的父节点。"
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);

& # 39;提示# 39;在将XAML转换为c#代码时,不能有逻辑或视觉上的父错误

不久前我遇到过类似的情况,我读到(=意识到)您不应该自己构建可视树,因为它是一个普通的DOM。相反,你应该使用模板,即使用代码(例如c#)创建模板,然后应用它。

换句话说,只需遵循frameworkelementfactory方式,尽管它是一个已弃用的API。我个人不喜欢依赖未经建议的东西,但它似乎是实现你需要的最好方法。

然而,"最好"的方法是使用XAML!

欢呼

相关文章: