简体   繁体   English

在WPF中将ComboBox数据绑定到ListView数据

[英]Binding ComboBox data to ListView Data in wpf

I am trying to generate a WPF modal containing combo box inside List views. 我正在尝试在列表视图内部生成一个包含组合框的WPF模态。 The combo-box will be generated dynamically and requires binding. 组合框将动态生成并需要绑定。

XAML Code XAML代码

<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. 我是C#和WPF的新手。

Looks like problem is in your binding expression for ComboBox 看起来问题出在您的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. 您不需要项目源中的相对源绑定,DataTemplate将应用于所有呈现的项目。 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. 如果仍然无法使用,请在Visual Studio输出窗口中查看,它会详细输出绑定错误。 You can share it here if that is the case. 如果是这种情况,您可以在这里分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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