使用项目在 WPF ListView 中动态填充 ComboBox [英] Dynamically Populate ComboBox Within A WPF ListView With Items

查看:29
本文介绍了使用项目在 WPF ListView 中动态填充 ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到数据源的 WPF ListView.在 ListView 中动态创建 ComboBoxes,我想将其绑定到另一个数据源以提供 Items,但 SelectedIndex 来自第一个数据源(请参阅下面的 XAML).

I have a WPF ListView which is bound to a data source. In the ListView are dynamically created ComboBoxes, which I would like to bind to another data source to provide the Items, but the SelectedIndex comes from the first data source (see XAML below).

目前,如果 ComboBoxItems 静态编码到 XAML(显示在下面的 XAML 中注释掉),则该功能可以正常工作.但是,我想动态提供 ComboBoxItems(字符串列表),因此我可以通过以下任一方式扩展列表:1)绑定到自定义类(请参阅下面的代码隐藏),或 2)在 WindowInitialized 中设置 DataSource或 WindowRendered 事件.

Currently, the functionality works fine if the ComboBoxItems are coded statically into the XAML (shown commented out in the XAML below). But, I want to provide the ComboBoxItems (list of strings) dynamically, so I can expand the list, by either 1) binding to the a custom class (see my code-behind below), or 2) setting the DataSource in the WindowInitialized or WindowRendered event(s).

我已经尝试了两种方法,但在这里我展示了我尝试过的第一种方法的示例.下面的 XAML 和 VB 代码没有错误,但组合框显示为空下拉列表.有什么想法吗?

I've tried both ways, but here I'm showing an example of the first method that I tried. The XAML and VB code below doesn't error, but the ComboBoxes show up with empty drop-downs. Any ideas?

此外,字符串列表实际上只是简单字符串的简单列表(但我想让它成为一个长列表).有没有更简单的方法来填充 ComboBox?(我个人认为第二种方法更简单,只需创建一个 VB List 并设置数据源,但结果相同)

Also, the list of strings is, really, just a simple list of simple strings (but I want to make it a long list). Is there an easier way to populate the ComboBox? (Personally, I thought the second method was quite simpler to just create a VB List and set the datasource, but that had the same result)

我的解决方案是,简单地在 ListView 的数据源中创建另一个 Property(OfType String) 并将 ComboBox 绑定到新属性.以下是新属性的外观:

My solution was to, simply, create another Property(OfType String) within the datasource of the ListView and bind the ComboBox to the new Property. Here's how the new Property looks:

    Public ReadOnly Property myList As List(Of String)
        Get
            Dim cboxList As New List(Of String)
            For...
                cboxList.Add(New String("..."))
            Next
            Return cboxList
        End Get
    End Property

XAML:

<ListView IsSynchronizedWithCurrentItem="True" Margin="11,5,0,0" x:Name="myList" HorizontalAlignment="Left" Width="130" BorderBrush="{x:Null}" BorderThickness="0" Background="White" Height="240" VerticalAlignment="Top" Grid.Column="1" >
    <ListView.View>
    <GridView AllowsColumnReorder="False">
        <GridViewColumn Header="Freq" Width="55">
        <GridViewColumn.CellTemplate>
            <DataTemplate>
            <ComboBox HorizontalContentAlignment="Center"
                x:Name="_ListFrequencies"
                ItemsSource="{Binding TestStrings}"
                DisplayMemberPath="TestString"
                IsEnabled="{Binding outCustomValue1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                SelectionChanged="_OnSelectionChanged"
                SelectedIndex="{Binding outCustomValue2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                FontSize="10"/>
                                    <!--
                                        <ComboBoxItem Content="test"/>
                                        <ComboBoxItem Content="test"/>
                                        <ComboBoxItem Content="test"/>
                                        -->
            </DataTemplate>
        </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
    </ListView.View>
</ListView>

代码隐藏

Public Class MyComboBoxItems

    Public Property TestStrings() As List(Of MyComboBoxStrings)

    Public Function MyComboBoxItems()
        TestStrings = New List(Of MyComboBoxStrings)
    End Function

End Class

Public Class MyComboBoxStrings

    Public ReadOnly Property TestString As String
        Get
            Return "test"
        End Get
    End Property

End Class

推荐答案

  1. 您的场景需要一个 ViewModel.

对应的C#代码(你可以用它作为伪代码):

Corresponding C# code ( you can use it as pseudo-code ) :

public class ViewModel
    {
        public List<MyComboBoxItems> items { get; set; }
        public int outCustomValue2 { get; set; }
        public bool outCustomValue1 { get; set; }

        public ViewModel()
        {
            items = new List<MyComboBoxItems>();
            items.Add(new MyComboBoxItems());
            items.Add(new MyComboBoxItems());
            items.Add(new MyComboBoxItems());
            items.Add(new MyComboBoxItems());
        }
    }

    public class MyComboBoxItems
    {
        public List<MyComboBoxStrings> TestStrings { get; set; }       

        public MyComboBoxItems()
        {
            TestStrings = new List<MyComboBoxStrings>();
            TestStrings.Add(new MyComboBoxStrings("val1"));
            TestStrings.Add(new MyComboBoxStrings("val1"));
            TestStrings.Add(new MyComboBoxStrings("val1"));
            TestStrings.Add(new MyComboBoxStrings("val1"));
        }
    }

    public class MyComboBoxStrings
    {
        string _testString;
        public string TestString { get { return _testString; } }

        public MyComboBoxStrings(string value)
        {
            _testString = value;
        }
    }

应用数据上下文:

 ViewModel vm = new ViewModel();
 myList.DataContext = vm;

这篇关于使用项目在 WPF ListView 中动态填充 ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆