views:

134

answers:

3

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.

+1  A: 

According to http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/289e0000-a3f7-440b-8235-c8ca36320dd4 you can insert Rows and Columns as children.

Sierrodc
A: 

Ok....that should work but how do you assign something, i.e. x or xValue, to a specific column? And any idea why I am getting the exception mentioned in the question?

PlTaylor
A: 

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;
    }
PlTaylor