1

I am populating a listview from code behind, why I am doing this scenario is, I need to create columns dynamically from code behind(one column,two,three....). My code looks like below

XAML Code :

<ListView  ItemsSource="{Binding}" Name="lvLOv" Width="400"></ListView>

C# Code:

Output:

enter image description here

But I need to place checkboxes before every row like as below(I need to do this in code behind like above code/ integrate the checkbox code in the above code). I need to place checkall option also in the header

public TestForm() { this.InitializeComponent(); Test(); }

public void Test()
{
    try
    {
        DataTable dt = new DataTable();

        //Create Columns
        dt.Columns.Add("Initial", typeof(string));
        dt.Columns.Add("Name", typeof(string));

        //Adding Rows
        for (int i = 0; i < 3; i++)
        {
            dt.Rows.Add("K" + i, "David" + i);
        }

        GridView gv = new GridView();


        // Create the GridView Columns
        foreach (DataColumn item in dt.Columns)
        {
            GridViewColumn gvc = new GridViewColumn();
            gvc.DisplayMemberBinding = new Binding(item.ColumnName);
            gvc.Header = item.ColumnName;
            gvc.Width = Double.NaN;
            gv.Columns.Add(gvc);
        }

        lvLOv.View = gv;

        //Binding to Listview
        lvLOv.DataContext = dt.DefaultView;

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}

Expected Output:

enter image description here

Can you please let me know how can I achieve this functionality

6
  • 2
    This is not how to write WPF. When using WPF, we do not programmatically add UI elements. Instead, we use data binding and manipulate data elements. See the Data Binding Overview page on MSDN for more information. Commented Nov 24, 2014 at 14:24
  • You have to assign ListView.ItemTemplate. It may not be easy in code behind (click, click). Commented Nov 24, 2014 at 14:24
  • Delete all that horrible code and use proper DataBinding and Data Templating. Commented Nov 24, 2014 at 14:29
  • Thanks for your suggestions, I know but I am getting different data(with different column count). I know the columns at run time then how can I bind those columns to listview Commented Nov 24, 2014 at 15:42
  • Why is this community so abrasive? Commented Oct 15, 2017 at 18:42

1 Answer 1

1

Its really easy just do this..

In Design view (Xaml):

<Window.Resources> 
   <DataTemplate x:Key="Chk_Field" DataType="{x:Type GridViewColumn}">
      <CheckBox IsChecked="{Binding chk}" />
   </DataTemplate>
</Window.Resources>

In Code behind:

GridView gridview = new GridView();
Window window = Application.Current.MainWindow;
DataTemplate s = (DataTemplate)window.FindResource("Chk_Field");
gridview.Columns.Add(new GridViewColumn { Header = "Head", CellTemplate = s });

By doing this you can change the celltemplate for each column in code behind..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.