在 C# 中构建数据模板

发布于 2024-09-24 09:20:20 字数 2673 浏览 2 评论 0原文

我正在尝试在 C# 中构建以下 DataTemplate

<DataTemplate x:Key="lbl">
        <!-- Grid 2x2 with black border -->
        <Border BorderBrush="Black">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <!-- x-coordinate -->
                <TextBlock Text="X=" />
                <TextBlock Grid.Column="1" Text="{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"/>
                <!-- y-coordinate -->
                <TextBlock Grid.Row="1" Text="Y=" />
                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Value, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}" />
            </Grid>
        </Border>
    </DataTemplate>

到目前为止,我无法弄清楚 Grid.ColumnDefinitions ,并且出现以下异常

FrameworkElementFactory 必须位于密封模板中才能执行此操作。

private static DataTemplate GetToolTipsDataTemplate()
{
        FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));

        FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock)); 
        x.SetBinding(TextBlock.TextProperty, new Binding("X="));
        grid.AppendChild(x);

        FrameworkElementFactory xValue = new FrameworkElementFactory(typeof(TextBlock));
        xValue.SetValue(TextBlock.TextProperty, "{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
        grid.AppendChild(xValue);

        FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock));
        y.SetBinding(TextBlock.TextProperty, new Binding("Y="));
        grid.AppendChild(y);

        FrameworkElementFactory yValue = new FrameworkElementFactory(typeof(TextBlock));
        yValue.SetValue(TextBlock.TextProperty, "{Binding Path=Values, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
        grid.AppendChild(yValue);

        FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
        border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black);
        border.AppendChild(grid);  

        DataTemplate dt = new DataTemplate {VisualTree = border};
        return dt;
    }

任何帮助将不胜感激。

I am trying to build the following DataTemplate in C#

<DataTemplate x:Key="lbl">
        <!-- Grid 2x2 with black border -->
        <Border BorderBrush="Black">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <!-- x-coordinate -->
                <TextBlock Text="X=" />
                <TextBlock Grid.Column="1" Text="{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"/>
                <!-- y-coordinate -->
                <TextBlock Grid.Row="1" Text="Y=" />
                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Value, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}" />
            </Grid>
        </Border>
    </DataTemplate>

I have gotten this far and I can't figure out the Grid.ColumnDefinitions and I am getting an exception of

FrameworkElementFactory must be in a sealed template for this operation.

private static DataTemplate GetToolTipsDataTemplate()
{
        FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));

        FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock)); 
        x.SetBinding(TextBlock.TextProperty, new Binding("X="));
        grid.AppendChild(x);

        FrameworkElementFactory xValue = new FrameworkElementFactory(typeof(TextBlock));
        xValue.SetValue(TextBlock.TextProperty, "{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
        grid.AppendChild(xValue);

        FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock));
        y.SetBinding(TextBlock.TextProperty, new Binding("Y="));
        grid.AppendChild(y);

        FrameworkElementFactory yValue = new FrameworkElementFactory(typeof(TextBlock));
        yValue.SetValue(TextBlock.TextProperty, "{Binding Path=Values, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}");
        grid.AppendChild(yValue);

        FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
        border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black);
        border.AppendChild(grid);  

        DataTemplate dt = new DataTemplate {VisualTree = border};
        return dt;
    }

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶色山野 2024-10-01 09:20:20

我走了一个不同的方向,除了装订上的格式之外,让它正常工作。

private static DataTemplate GetToolTipsDataTemplate()
    {
        XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";
        XNamespace my = "clr-namespace:C1.WPF.C1Chart;assembly=C1.WPF.C1Chart";
        XElement xDataTemplate =
            new XElement(ns + "DataTemplate",
                new XElement(ns + "Border",
                    new XAttribute("BorderBrush", "Black"),
                    new XElement(ns + "Grid",
                        new XElement(ns + "Grid.ColumnDefinitions",
                            new XElement(ns + "ColumnDefinition"),
                            new XElement(ns + "ColumnDefinition")),
                        new XElement(ns + "Grid.RowDefinitions",
                            new XElement(ns + "RowDefinition"),
                            new XElement(ns + "RowDefinition")),
                        new XElement(ns + "TextBlock",
                            new XAttribute("Text", "X=")),
                        new XElement(ns + "TextBlock",
                            new XAttribute("Text", "{Binding Path=[XValues]}"), //, Converter={" + x +"Static " + my + "Converters.Format}, ConverterParameter=#.##}"),
                            new XAttribute("Grid.Column", "1")),
                        new XElement(ns + "TextBlock",
                            new XAttribute("Text", "Y="),
                            new XAttribute("Grid.Row", "1")),
                        new XElement(ns + "TextBlock",
                            new XAttribute("Text", "{Binding Path= [Values]}"), //, Converter={" + x +":Static " + my + ":Converters.Format}, ConverterParameter=#.##}"),
                            new XAttribute("Grid.Column", "1"),
                            new XAttribute("Grid.Row", "1"))
                            )));

        StringReader sr = new StringReader(xDataTemplate.ToString());
        XmlReader xr = XmlReader.Create(sr);

        DataTemplate dataTemplateObect = System.Windows.Markup.XamlReader.Load(xr) as DataTemplate;

        return dataTemplateObect;
    }

I have gone a different direction and gotten it to work except for the formatting on the binding.

private static DataTemplate GetToolTipsDataTemplate()
    {
        XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";
        XNamespace my = "clr-namespace:C1.WPF.C1Chart;assembly=C1.WPF.C1Chart";
        XElement xDataTemplate =
            new XElement(ns + "DataTemplate",
                new XElement(ns + "Border",
                    new XAttribute("BorderBrush", "Black"),
                    new XElement(ns + "Grid",
                        new XElement(ns + "Grid.ColumnDefinitions",
                            new XElement(ns + "ColumnDefinition"),
                            new XElement(ns + "ColumnDefinition")),
                        new XElement(ns + "Grid.RowDefinitions",
                            new XElement(ns + "RowDefinition"),
                            new XElement(ns + "RowDefinition")),
                        new XElement(ns + "TextBlock",
                            new XAttribute("Text", "X=")),
                        new XElement(ns + "TextBlock",
                            new XAttribute("Text", "{Binding Path=[XValues]}"), //, Converter={" + x +"Static " + my + "Converters.Format}, ConverterParameter=#.##}"),
                            new XAttribute("Grid.Column", "1")),
                        new XElement(ns + "TextBlock",
                            new XAttribute("Text", "Y="),
                            new XAttribute("Grid.Row", "1")),
                        new XElement(ns + "TextBlock",
                            new XAttribute("Text", "{Binding Path= [Values]}"), //, Converter={" + x +":Static " + my + ":Converters.Format}, ConverterParameter=#.##}"),
                            new XAttribute("Grid.Column", "1"),
                            new XAttribute("Grid.Row", "1"))
                            )));

        StringReader sr = new StringReader(xDataTemplate.ToString());
        XmlReader xr = XmlReader.Create(sr);

        DataTemplate dataTemplateObect = System.Windows.Markup.XamlReader.Load(xr) as DataTemplate;

        return dataTemplateObect;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文