简体   繁体   中英

Binding ComboBox data to ListView Data in wpf

I am trying to generate a WPF modal containing combo box inside List views. The combo-box will be generated dynamically and requires binding.

XAML Code

<ListView Height="291" HorizontalAlignment="Left" Margin="12,196,0,0" Name="filterByList" VerticalAlignment="Top" Width="303" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Height="23" HorizontalAlignment="Left" Margin="0,50,34,0" Name="filterName" Text="{Binding DisplayName}" VerticalAlignment="Top" />
                <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,50,34,0" Name="filterValues" VerticalAlignment="Top" Width="107" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=filterByValues}" IsEnabled="True" SelectedIndex="0" SelectedValuePath="filterByValues" DisplayMemberPath="filterByValues" />
             </StackPanel>
         </DataTemplate>
     </ListView.ItemTemplate>
</ListView>

Code-Behind

     List<Dimension> dimensionsData = new List<Dimension>();
            List<filterByValues> filterByValuesData = new List<filterByValues>();
            JArray filterByObject = JArray.Parse("[ { 'DisplayName': 'Fund', 'Values': [ 'FundA', 'FundB', 'FundC' ] }, { 'DisplayName': 'Sector', 'Values': [ 'SectorA', 'SectorB', 'SectorC' ] }, { 'DisplayName': 'Country', 'Values': [ 'CountryA', 'CountryB', 'CountryC' ] } ]");

            foreach (JObject value in filterByObject)
            {
                filterByValuesData = new List<filterByValues>();
                String JsonName = (String)value.GetValue("JsonName");
                String DisplayName = (String)value.GetValue("DisplayName");
                JArray Values = (JArray)value.GetValue("Values"); 
                foreach (var item in Values)
                {
                    filterByValuesData.Add(new filterByValues((string)item)); 
                }
                dimensionsData.Add(new Dimension { DisplayName = DisplayName, filterByValues = filterByValuesData }); 
            }
            filterByList.ItemsSource = dimensionsData;

 public class Dimension
    {
        public string DisplayName { get; set; }
        public List<filterByValues> filterByValues { get; set; }
    }

  public class filterByValues{
      public string filter{ get; set; }
      public filterByValues(String val) {
          filter = val;
      }
    }

The combo box generated is empty. I am very new to C# and WPF.

Looks like problem is in your binding expression for ComboBox

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,50,34,0" Name="filterValues" VerticalAlignment="Top" Width="107" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=filterByValues}" IsEnabled="True" SelectedIndex="0" SelectedValuePath="filterByValues" DisplayMemberPath="filterByValues" />

You don't need a relative source binding in item source, the DataTemplate will be applied to all items being rendered. You can try

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,50,34,0" Name="filterValues" VerticalAlignment="Top" Width="107" ItemsSource="{Binding filterByValues}" IsEnabled="True" SelectedIndex="0" SelectedValuePath="filter" DisplayMemberPath="filter" />

If it still don't work, have a look on the Visual Studio output window, It outputs the binding error in details. You can share it here if that is the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM