locked
bind a comboBox to a collection RRS feed

  • Question

  • Hi,
    in a Gridview I would display and bind data to a DataTable.
    I add into GridView a TextBox and a ComboBox.
    ComboBox items derives from a XML file.
    I fill the DataTable from file and in the code-behind I bind it to the GridView’s DataContext.
    I bind the Text of TextBox to the field of the table and data are really displayed, but ComboBox Text is empty (Field exists in the Table) while the items in the dropDown list are filled correctly (from XML File).
    Which property I missed?

    I tried with SelectedPath or SelectedItem but no text is displayed in comboBox.
    The ComboBox's items can be different from the value of the field to bind.

    <Window x:Class="GridView.Window1"

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

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

        xmlns:this="clr-namespace:GridView"

        Title="Window1" Height="300" Width="353">

        <Window.Resources>

            <CollectionViewSource x:Key="CSVCollection" />

            <XmlDataProvider  x:Key="ShippingProvider" x:Name="ShippingProvider1"

                        Source="Shippings.xml"

                        XPath="Shippings" />

            <DataTemplate x:Key="ShippingItemTemplate">

                <TextBlock Text="{Binding XPath=Description}"/>

            </DataTemplate>

        </Window.Resources>

     

        <Grid>

            <Button Height="27" IsDefault="False" Margin="12,12,0,0" Name="buttonDBFolder" ToolTip="Choose Folder..." VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Click="buttonCSVFile_Click">

            Choose file...</Button>

            <ListBox Margin="20,64,25,0" Name="listBoxCSVFiles" ToolTip="List CSV Files in Current Folder" IsEnabled="False" SelectionChanged="listBoxCSVFiles_SelectionChanged" VerticalContentAlignment="Center" Height="63" VerticalAlignment="Top" />

            <TextBox Height="46" Margin="88,12,21,0" Name="textBoxCSVFolder" VerticalAlignment="Top" TextWrapping="Wrap"/>

     

    <!—The code is here-->

            <ListView Margin="20,133,25,62" Name="CSVlistView" ItemsSource="{Binding}" DisplayMemberPath="ChildItem"

                      DataContext="{Binding Source={StaticResource CSVCollection}}">

                <ListView.View>

                    <GridView x:Name="CSVGridView"> 

                       <GridViewColumn Header="MyShippingServiceOptions">

                        <GridViewColumn.CellTemplate>

                            <DataTemplate>

                                    <!-- Items are properly filled-->

                                    <ComboBox Height="21"  Name="shippingComboBox"  IsEditable="False" VerticalAlignment="Bottom" Margin="71,0,63,12"

                                       ItemsSource="{Binding Source={StaticResource ShippingProvider}, XPath=Shipping}"

                                      ItemTemplate="{StaticResource ShippingItemTemplate}" >

    <!—Which is the property to bind to the Collection?-->

                                    </ComboBox>

                            </DataTemplate>

                        </GridViewColumn.CellTemplate>

                            </GridViewColumn>

                    <GridViewColumn Header="MyTitle">

                        <GridViewColumn.CellTemplate>

    <!—TextBox binding works-->

     

                            <DataTemplate>

                                <TextBox Name="titleTextBox" Text="{Binding Title}"> </TextBox>

                            </DataTemplate>

                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>

                    </GridView>

                </ListView.View>

     

            </ListView>

        </Grid>

    </Window>

     

     

    In the code-behind:

     

     

    // CSVTable is Filled

    CSVCollection = (CollectionViewSource)this.FindResource("CSVCollection");

    CSVCollection.Source=((IListSource)CSVTable).GetList();

     


    I’m waiting for your precious advice.

    Best Regards,
    Mauro


    Mauro
    Wednesday, January 27, 2010 6:11 PM

Answers

  • Hi MauroGv,

    Based on my understanding, you are binding a combo box with the data source which is from a XML file. And it seems that the combo box does not display correctly.

    When you use binding with XML data source, the use of the XPath property in the Binding object instead of the Path property, and the @ symbol to designate binding to an XML attribute.

    Here is a simple solution demonstrating how to bind with XML data source:
    XML file: XMLData.xml
    <?xml version="1.0" encoding="utf-8" ?>

    <Family xmlns="http://sells.com">

      <Person Name="Kevin" Age="22"></Person>

      <Person Name="Yang" Age="25"></Person>

      <Person Name="Lee" Age="32"></Person>

      <Person Name="John" Age="27"></Person>

    </Family>
    XAML
    <Window x:Class="DataBinding.XMLDataBinding"

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

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

        Title="XMLDataBinding" Height="300" Width="300">

        <Window.Resources>

            <XmlDataProvider x:Key="family" Source="XMLData.xml" XPath="/sb:Family/sb:Person" >

                <XmlDataProvider.XmlNamespaceManager>

                    <XmlNamespaceMappingCollection>

                        <XmlNamespaceMapping Prefix="sb" Uri="http://sells.com"></XmlNamespaceMapping>

                    </XmlNamespaceMappingCollection>

                </XmlDataProvider.XmlNamespaceManager>

            </XmlDataProvider>

            <DataTemplate x:Key="comboboxtemplate">

                <TextBlock Text="{Binding XPath=@Name}" Background="AliceBlue"></TextBlock>

            </DataTemplate>

        </Window.Resources>

        <StackPanel DataContext="{StaticResource family}">

            <ComboBox ItemsSource="{Binding}"

                      ItemTemplate="{StaticResource comboboxtemplate}"

                      >

            </ComboBox>

        </StackPanel>

    </Window>


    Hope this helps. If you still have any question about this, please feel free to reply.

    Best regards,
    Kevin

    • Marked as answer by Linda Liu Wednesday, February 3, 2010 6:37 AM
    Tuesday, February 2, 2010 7:24 AM

All replies

  • Hi MauroGv,

    Based on my understanding, you are binding a combo box with the data source which is from a XML file. And it seems that the combo box does not display correctly.

    When you use binding with XML data source, the use of the XPath property in the Binding object instead of the Path property, and the @ symbol to designate binding to an XML attribute.

    Here is a simple solution demonstrating how to bind with XML data source:
    XML file: XMLData.xml
    <?xml version="1.0" encoding="utf-8" ?>

    <Family xmlns="http://sells.com">

      <Person Name="Kevin" Age="22"></Person>

      <Person Name="Yang" Age="25"></Person>

      <Person Name="Lee" Age="32"></Person>

      <Person Name="John" Age="27"></Person>

    </Family>
    XAML
    <Window x:Class="DataBinding.XMLDataBinding"

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

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

        Title="XMLDataBinding" Height="300" Width="300">

        <Window.Resources>

            <XmlDataProvider x:Key="family" Source="XMLData.xml" XPath="/sb:Family/sb:Person" >

                <XmlDataProvider.XmlNamespaceManager>

                    <XmlNamespaceMappingCollection>

                        <XmlNamespaceMapping Prefix="sb" Uri="http://sells.com"></XmlNamespaceMapping>

                    </XmlNamespaceMappingCollection>

                </XmlDataProvider.XmlNamespaceManager>

            </XmlDataProvider>

            <DataTemplate x:Key="comboboxtemplate">

                <TextBlock Text="{Binding XPath=@Name}" Background="AliceBlue"></TextBlock>

            </DataTemplate>

        </Window.Resources>

        <StackPanel DataContext="{StaticResource family}">

            <ComboBox ItemsSource="{Binding}"

                      ItemTemplate="{StaticResource comboboxtemplate}"

                      >

            </ComboBox>

        </StackPanel>

    </Window>


    Hope this helps. If you still have any question about this, please feel free to reply.

    Best regards,
    Kevin

    • Marked as answer by Linda Liu Wednesday, February 3, 2010 6:37 AM
    Tuesday, February 2, 2010 7:24 AM
  • Hi Kevin thanks for your reply.
    I think your answer solves a part fo my problem.
    I believe that my problem concerns something like multibinding.
    The comboBox's items came from XMLFiles and as your code shows I can bind with their, but the value of combobox, what is displayed in the textbox of the combo, came from another Source, the CollectionViewSource, and I would bind the value with it. The comboBox is inside a ListView/GridView, and the DataContext is the the CollectionViewSource. The source is a Datatable.

    Thanks,
    Mauro


    Mauro
    Wednesday, February 3, 2010 9:18 AM
  • Hi MauroGv,

    Based on my understanding, your combo box items source comes from xml data, and you may have a list view to bind data comes from a DataTable. The combo box is used to bind one property of the data item in the data table. If I’m off base, please feel free to let me know.

    So, in this situation, you should bind the SelectedValue property to the data item property, and set the SelectValuePath and DisplayMemberPath property to the field in xml.

    Here is a solution for you as follows:
    XML (NamedAges.xml)
    <?xml version="1.0" encoding="utf-8" ?>

    <NamedAges xmlns="http://NamedAges.com">

      <NamedAge AgeId="1" AgeName="One"></NamedAge>

      <NamedAge AgeId="2" AgeName="Two"></NamedAge>

      <NamedAge AgeId="3" AgeName="Three"></NamedAge>

      <NamedAge AgeId="4" AgeName="Four"></NamedAge>

      <NamedAge AgeId="5" AgeName="Five"></NamedAge>

      <NamedAge AgeId="6" AgeName="Six"></NamedAge>

      <NamedAge AgeId="7" AgeName="Seven"></NamedAge>

      <NamedAge AgeId="8" AgeName="Eight"></NamedAge>

      <NamedAge AgeId="9" AgeName="Nine"></NamedAge>

      <NamedAge AgeId="10" AgeName="Ten"></NamedAge>

      <NamedAge AgeId="11" AgeName="Eleven"></NamedAge>

      <NamedAge AgeId="12" AgeName="Twelve"></NamedAge>

      <NamedAge AgeId="13" AgeName="Thriteen"></NamedAge>

      <NamedAge AgeId="14" AgeName="Fourteen"></NamedAge>

      <NamedAge AgeId="15" AgeName="Fifteen"></NamedAge>

      <NamedAge AgeId="16" AgeName="Sixteen"></NamedAge>

      <NamedAge AgeId="17" AgeName="Seventeen"></NamedAge>

      <NamedAge AgeId="18" AgeName="Eighteen"></NamedAge>

      <NamedAge AgeId="19" AgeName="Nineteen"></NamedAge>

      <NamedAge AgeId="20" AgeName="Twenty"></NamedAge>

    </NamedAges>

    XAML
    <Window x:Class="DataBinding.ComboBoxInListViewItemBinding"

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

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

        xmlns:local="clr-namespace:DataBinding"

        Title="ComboBoxInListViewItemBinding" Height="300" Width="300">

        <Window.Resources>

            <XmlDataProvider x:Key="namedAges" Source="NamedAges.xml" XPath="/sb:NamedAges/sb:NamedAge" >

                <XmlDataProvider.XmlNamespaceManager>

                    <XmlNamespaceMappingCollection>

                        <XmlNamespaceMapping Prefix="sb" Uri="http://NamedAges.com"></XmlNamespaceMapping>

                    </XmlNamespaceMappingCollection>

                </XmlDataProvider.XmlNamespaceManager>

            </XmlDataProvider>

            <DataTemplate x:Key="comboboxtemplate">

                <TextBlock Text="{Binding XPath=@AgeName}" Background="AliceBlue"></TextBlock>

            </DataTemplate>

            <local:People x:Key="persons">

                <local:Person Name="Kevin" Age="12"></local:Person>

                <local:Person Name="Yang" Age="18"></local:Person>

                <local:Person Name="Lee" Age="16"></local:Person>

                <local:Person Name="Micheal" Age="5"></local:Person>

            </local:People>

        </Window.Resources>

        <StackPanel DataContext="{StaticResource namedAges}">

            <ListView Height="200" DataContext="{StaticResource persons}" ItemsSource="{Binding}">

                <ListView.ItemTemplate>

                    <DataTemplate>

                        <TextBlock>

                            <TextBlock>Name:</TextBlock>

                            <TextBlock Text="{Binding Path=Name}"></TextBlock>(

                            <TextBlock Text="{Binding Path=Age}"></TextBlock>

                            )

                            <ComboBox ItemsSource="{Binding Source={StaticResource namedAges}}"

                                      SelectedValue="{Binding Path=Age}"

                                      SelectedValuePath="@AgeId"

                                      DisplayMemberPath="@AgeName"

                                      >

                            </ComboBox>

                        </TextBlock>

                    </DataTemplate>

                </ListView.ItemTemplate>

            </ListView>

        </StackPanel>

    </Window>


    Code-behind
        public class Person

        {

            public string Name

            { get; set; }

     

            public int Age

            { get; set; }

        }

        public class People : System.Collections.ObjectModel.ObservableCollection<Person> { }


    Hope this helps. If you still have any question about this issue, please feel free to reply.

    Best regards,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, February 3, 2010 11:28 AM
  • Hi Linda,
    it's a very good and useful example.
    Thanks very much,
    Mauro
    Mauro
    Friday, February 19, 2010 11:00 AM
  • How would I go about doing this same thing except with a DataTable for the list and a class object property for the selectedValue?
    Thursday, May 6, 2010 1:51 PM