Question

at the moment I ‘am working on a little program that shows the vertices of a 3D-modell as a table (datagrid). I’ve written a class “vertex” witch contains the coordinates, color etc. as 1x3 float-arrays. When I bind the list of Vertices to the datagrid it looks as follows (pictures not embbeded because I didn't got the needed reputation):

Current view of the window

It should look like this:

Like it should look (made in Word)

As you can see in the last column, I’ve already tried to embed the array as an additional datagrid inside the first one, this failed probably because the datagrid didn’t has a column template for datagrids. After that I tried to create a template like in this MSDN-article: http:// msdn.microsoft.com/en-us/library/cc903950(v=vs.95).aspx

The created inner datagrids got the right dimensions but stay empty, probably a binding mistake.

Sourcecode: XAML:

<Window x:Class="Mod2_Leser.Tabellenanzeige"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Tabellenanzeige" Height="480" Width="640" Background="AliceBlue">
<Window.Resources>
    <DataTemplate x:Key="DGCellTemplate">
        <DataGrid ItemsSource="{Binding Koordinaten}" />
    </DataTemplate>
    <DataTemplate x:Key="DGCellEditingTemplate">
        <DataGrid ItemsSource="{Binding Koordinaten, Mode=TwoWay}" />
    </DataTemplate>
</Window.Resources>
<Grid>
    <ScrollViewer HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" 
                  VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
        <Grid x:Name="Grid2">
            <Grid.RowDefinitions>
                <!--<RowDefinition Height="35"/>-->
                <RowDefinition Height="Auto" MinHeight="80" />
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <DataGrid x:Name="DG" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" IsReadOnly="True"
                      Margin="10,10,10,0" AutoGenerateColumns="True" CanUserAddRows="False" CanUserDeleteRows="False" AutoGeneratingColumn="DG_AutoGeneratingColumn"/>
            <Button x:Name="BClose" Content="Schließen" Grid.Row="1" Height="30" Margin="10,10,10,10" Click="BClose_Click"/>
        </Grid>
    </ScrollViewer>
</Grid>
</Window>

C#

public Tabellenanzeige(KonBereich KB, bool? vertinsttria) {
        InitializeComponent();
        KB.UpdateDGs();
        if (vertinsttria == true) {
            #region Vertexe
            Title = "Vertex-Ansicht";
            Binding DGBinding = new Binding();
            DGBinding.Source = KB.VertexListe;
            DGBinding.Mode = BindingMode.OneWay;
            DG.AutoGenerateColumns = true;
            DG.SetBinding(TextBlock.TextProperty, DGBinding);
            if (DG.ItemsSource == null) {
                DG.ItemsSource = KB.VertexListe;
                DG.Items.Refresh();
            } else {
                DG.ItemsSource = null;
                DG.ItemsSource = KB.VertexListe;
                DG.Items.Refresh();
            }
            #endregion
        }
    }

private void DG_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.PropertyName == "Koordinaten")
        {
            DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
            templateColumn.Header = e.PropertyName;
            DataTemplate DT = new DataTemplate();
            templateColumn.CellTemplate = (DataTemplate)Resources["DGCellTemplate"];
            templateColumn.CellEditingTemplate = (DataTemplate)Resources["DGCellEditingTemplate"];
            templateColumn.SortMemberPath = "Koordinaten";
            e.Column = templateColumn;
        }
 }

I set the ItemsSource in code-behind because depending on the vertinsttria-variable different things are displayed in the datagrid.

Does anybody know how I can reach the aimed way of showing the data?

Greetings from Germany

Trainfan884

Was it helpful?

Solution

A DataGrid inside a DataGrid is not the way to go, use an ItemsControl instead :

<Window x:Class="WpfApplication15.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpfApplication15="clr-namespace:WpfApplication15"
        Title="MainWindow"
        Width="618"
        Height="439"
        mc:Ignorable="d">
    <Grid>
        <Grid.Resources />
        <DataGrid x:Name="DataGrid1"
                  AutoGenerateColumns="False"
                  ItemsSource="{Binding }"
                  d:DataContext="{d:DesignInstance wpfApplication15:MyObject}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Points">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding Points}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Code:

using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApplication15
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();


            var objects = new ObservableCollection<MyObject>(new[]
            {
                new MyObject {Name = "Object1", Points = new float[] {1, 2}},
                new MyObject {Name = "Object2", Points = new float[] {3, 4}}
            });
            DataContext = objects;
        }
    }

    internal class MyObject
    {
        public Single[] Points { get; set; }
        public string Name { get; set; }
    }
}

enter image description here

But now you have another problem : how to correctly present it using a mathematical notation ?

Some hints :

library for visualizing mathematical equations (like an equation editor)

How to render a formula in WPF or WinForms

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top