使用项目动态填充 WPF ListView 中的组合框

2023-12-07

我有一个绑定到数据源的 WPF ListView。在 ListView 中是动态创建的 ComboBox,我想将其绑定到另一个数据源以提供项目,但 SelectedIndex 来自第一个数据源(请参见下面的 XAML)。

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

我已经尝试了两种方法,但在这里我展示了我尝试的第一种方法的示例。下面的 XAML 和 VB 代码不会出错,但组合框显示为空下拉菜单。有任何想法吗?

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

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

    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#代码(您可以将其用作伪代码):

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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用项目动态填充 WPF ListView 中的组合框 的相关文章

随机推荐