locked
Listview with ComboBox RRS feed

  • Question

  • Hi,

    I am using a listview which has the combobox.
    I bind the listview properly but i am not able to bind the combobox of listview from my code behind file.

    Code Snippet

    This what i am doing in my XAML File

    <DataTemplate x:Key="template">

       <ComboBox Name="cmbAgent"  DisplayMemberPath="Agents"  ItemsSource="{Binding}"        IsSynchronizedWithCurrentItem="False"  HorizontalAlignment="Center" Width="80">      </ComboBox>

     </DataTemplate>


    <ListView Name="ListView1" ItemsSource="{Binding}">
                <ListView.View>               
                    <GridView>

                   <GridViewColumn DisplayMemberBinding="{Binding Path=SetupStepName}"     Header="Description" Width="200"/>

                        <GridViewColumn Header ="Agent" CellTemplate="{StaticResource template}" Width="100"/>                 
                    </GridView>
                </ListView.View>
            </ListView>

    Code Snippet

    This my .cs File
    I load the dataset
    dsFormLoad = ObjParameterData.GetIssuerAgent(1149);
    I bind the dataset to the dataview which will bind to the listview

    dvFormLoad = dsFormLoad.Tables[0].DefaultView;
    my another table in dataset which will bind to another dataview name dvAgent

    dvAgent = dsFormLoad.Tables[1].DefaultView;
    Now i bind the first dataview to the Listview like

     ListView1.DataContext = dvFormLoad;

    Now I want to bind the another dataview to the combobox which is inside the listview.
    How can i achieve this.
    i am not getting the name of  combobox to bind.


    Saturday, May 24, 2008 10:14 AM

Answers

  • The following example shows how to do this. It may helps for you.

     

    Code Snippet

    <Window

       x:Class="ForumProjects.MainWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       x:Name="Window" Title="MainWindow" Width="800" Height="600">

        <ListView ItemsSource="{Binding Products, ElementName=Window}">

            <ListView.View>

                <GridView>

                    <GridViewColumn DisplayMemberBinding="{Binding ProductName}"/>

                    <GridViewColumn>

                        <GridViewColumn.CellTemplate>

                            <DataTemplate>

                                <ComboBox ItemsSource="{Binding Categories, ElementName=Window}"

                                        DisplayMemberPath="CategoryName"

                                        SelectedValuePath="CategoryID"

                                        SelectedValue="{Binding ProductCategory}"/>

                            </DataTemplate>

                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>

                </GridView>

            </ListView.View>

        </ListView>

    </Window>

    namespace ForumProjects

    {

        public partial class MainWindow : Window

        {

            public MainWindow()

            {

                this.Products = LoadProducts();

                this.Categories = LoadCategories();

     

                InitializeComponent();

            }

     

            public DataTable Products { get; private set; }

     

            public DataTable Categories { get; private set; }

     

            private static DataTable LoadProducts()

            {

                DataTable table = new DataTable();

                table.Columns.Add("ProductName", typeof(string));

                table.Columns.Add("ProductCategory", typeof(int));

                table.Rows.Add("Product A", 2);

                table.Rows.Add("Product B", 1);

                table.Rows.Add("Product C", 2);

                return table;

            }

     

            private static DataTable LoadCategories()

            {

                DataTable table = new DataTable();

                table.Columns.Add("CategoryID", typeof(int));

                table.Columns.Add("CategoryName", typeof(string));

                table.Rows.Add(1, "Category 1");

                table.Rows.Add(2, "Category 2");

                return table;

            }

        }

    }

     

     

    Best Regards,

    Wei Zhou

    Tuesday, May 27, 2008 6:30 AM

All replies

  • The following example shows how to do this. It may helps for you.

     

    Code Snippet

    <Window

       x:Class="ForumProjects.MainWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       x:Name="Window" Title="MainWindow" Width="800" Height="600">

        <ListView ItemsSource="{Binding Products, ElementName=Window}">

            <ListView.View>

                <GridView>

                    <GridViewColumn DisplayMemberBinding="{Binding ProductName}"/>

                    <GridViewColumn>

                        <GridViewColumn.CellTemplate>

                            <DataTemplate>

                                <ComboBox ItemsSource="{Binding Categories, ElementName=Window}"

                                        DisplayMemberPath="CategoryName"

                                        SelectedValuePath="CategoryID"

                                        SelectedValue="{Binding ProductCategory}"/>

                            </DataTemplate>

                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>

                </GridView>

            </ListView.View>

        </ListView>

    </Window>

    namespace ForumProjects

    {

        public partial class MainWindow : Window

        {

            public MainWindow()

            {

                this.Products = LoadProducts();

                this.Categories = LoadCategories();

     

                InitializeComponent();

            }

     

            public DataTable Products { get; private set; }

     

            public DataTable Categories { get; private set; }

     

            private static DataTable LoadProducts()

            {

                DataTable table = new DataTable();

                table.Columns.Add("ProductName", typeof(string));

                table.Columns.Add("ProductCategory", typeof(int));

                table.Rows.Add("Product A", 2);

                table.Rows.Add("Product B", 1);

                table.Rows.Add("Product C", 2);

                return table;

            }

     

            private static DataTable LoadCategories()

            {

                DataTable table = new DataTable();

                table.Columns.Add("CategoryID", typeof(int));

                table.Columns.Add("CategoryName", typeof(string));

                table.Rows.Add(1, "Category 1");

                table.Rows.Add(2, "Category 2");

                return table;

            }

        }

    }

     

     

    Best Regards,

    Wei Zhou

    Tuesday, May 27, 2008 6:30 AM
  • Thanks  Wei Zhou

    Its working fine

    Tuesday, May 27, 2008 8:10 AM