Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:29:10+00:00 2026-05-23T09:29:10+00:00

I am working on a WPF application in C# (.NET 4.0) where I have

  • 0

I am working on a WPF application in C# (.NET 4.0) where I have a ListView with a GridView that has two columns.

I dynamically want to add rows (in code). My dilemma is that only the first column will have regular text added to it. The second column will have an object that includes a multi column Grid with TextBlocks. (see link http://imageshack.us/photo/my-images/803/listview.png/)

If I do what you would normally do when you want to enter text in all columns (ie. DisplayMemberBinding) all I get in the second column is the text “System.Windows.Grid”, which obviously isn’t what I want.

For reference if I just try to add the Grid object (with the TextBlocks) with the code listView1.Items.Add(grid1) (not using DisplayMemberBinding) the object gets added to the second column only (with the first column being blank) and not how it normally works with text where the same text ends up in all columns.

I hope my question is detailed enough and any help with this would be much appreciated.

EDIT:

I have tried the following code, howeever every time I click the button to add a new row every single row gets updated with the same datatemplate. (ie. the second column always shows the same data on every row.)

xaml:

<Window x:Class="TEST.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="AAA"  Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="grid1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="374*" />
        <ColumnDefinition Width="129*" />
    </Grid.ColumnDefinitions>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="21,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="button1_Click" />
</Grid>

code:

            public partial class MainWindow : Window
{

    ListView listView1 = new ListView();
    GridViewColumn viewCol2 = new GridViewColumn();

    public MainWindow()
    {
        InitializeComponent();

        Style style = new Style(typeof(ListViewItem));
        style.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty,
            HorizontalAlignment.Stretch));
        listView1.ItemContainerStyle = style;

        GridView gridView1 = new GridView();
        listView1.View = gridView1;
        GridViewColumn viewCol1 = new GridViewColumn();
        viewCol1.Header = "Option";
        gridView1.Columns.Add(viewCol1);
        viewCol2.Header = "Value";
        gridView1.Columns.Add(viewCol2);
        grid1.Children.Add(listView1);
        viewCol1.DisplayMemberBinding = new Binding("Option");
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        DataTemplate dataTemplate = new DataTemplate();

        FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(Grid));

        Random random = new Random();
        int cols = random.Next(1, 6);
        int full = 100;
        for (int i = 0; i < cols; i++)
        {
            FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
            int partWidth = random.Next(0, full);
            full -= partWidth;
            col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(partWidth, GridUnitType.Star));
            spFactory.AppendChild(col1);
        }
        if (full > 0)
        {
            FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
            col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(full, GridUnitType.Star));
            spFactory.AppendChild(col1);
        }
        for (int i = 0; i < cols; i++)
        {
            FrameworkElementFactory text1 = new FrameworkElementFactory(typeof(TextBlock));
            SolidColorBrush sb1 = new SolidColorBrush();
            switch (i)
            {
                case 0:
                    sb1.Color = Colors.Blue;
                    break;
                case 1:
                    sb1.Color = Colors.Red;
                    break;
                case 2:
                    sb1.Color = Colors.Yellow;
                    break;
                case 3:
                    sb1.Color = Colors.Green;
                    break;
                case 4:
                    sb1.Color = Colors.Purple;
                    break;
                case 5:
                    sb1.Color = Colors.Pink;
                    break;
                case 6:
                    sb1.Color = Colors.Brown;
                    break;
            }
            text1.SetValue(TextBlock.BackgroundProperty, sb1);
            text1.SetValue(Grid.ColumnProperty, i);
            spFactory.AppendChild(text1);
        }
        if (full > 0)
        {
            FrameworkElementFactory text1 = new FrameworkElementFactory(typeof(TextBlock));
            SolidColorBrush sb1 = new SolidColorBrush(Colors.Black);
            text1.SetValue(TextBlock.BackgroundProperty, sb1);
            text1.SetValue(Grid.ColumnProperty, cols);
            spFactory.AppendChild(text1);
        }

        dataTemplate.VisualTree = spFactory;

        viewCol2.CellTemplate = dataTemplate;


        int rows = listView1.Items.Count + 1;
        listView1.Items.Add(new { Option = "Row " + rows });

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:29:10+00:00Added an answer on May 23, 2026 at 9:29 am

    You do not have to use the DisplayMemberBinding, for complex content you can use the CellTemplate, e.g.:

    <ListView ItemsSource="{Binding Data}">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <!-- This should display your grid if it is a property on your item called GridProperty -->
                            <ContentControl Content="{Binding GridProperty}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a WPF application which has a treeview that represents an
I have a project I'm working on that requires our WPF application read SMS
I am currently working on this application(C#,WPF,LINQ2XML,.NET 4.0) that displays details for 'Animals'. An
I have a WPF application using MVVM that utilises TreeViewItemViewModels to simplify working with
I have a WPF application that is targeting the .NET Framework 4.0 Client Profile.
I'm working on a WPF application that sometimes exhibits odd problems and appears to
I'm working on a WPF application which should be utilizable with two monitors. In
I'm building a WPF application and working with the MVVM pattern. I have 4
I'm currently working out the layout of a WPF Application and seem to have
I have a WPF application (.NET Framework 4) with a custom window-border. I've disabled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.