动态填充组合框在一个WPF的ListView随着项目 [英] Dynamically Populate ComboBox Within A WPF ListView With Items

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

问题描述

我有一个绑定到数据源的一个WPF的ListView。在动态创建组合框ListView控件,我想绑定到另一个数据源提供的项目,但SelectedIndex的来自于第一个数据源(见下面的XAML)。

目前,如果ComboBoxItems是codeD静态到XAML(显示在下面的XAML注释掉)功能工作正常。但是,我想提供ComboBoxItems(字符串列表)动态,这样我就可以展开列表,方法是1)绑定到一个自定义类(见我的code-后面下面),或2)设定DataSource的在WindowInitialized或WindowRendered事件(S)。

我试图左右逢源,但在这里我展示,我尝试的第一个方法的一个例子。在XAML和VB code下面没有错误,但是组合框显示了空的下拉菜单。任何想法?

此外,字符串列表是,真的,只是简单的一个简单的字符串列表(但我想使它成为一个长长的清单)。是否有填充组合框更简单的方法? (我个人认为第二种方法是相当简单的只需要创建一个VB列表和设置数据源,但有相同的结果)

编辑:我的解决方案是,简单地说,创建的ListView的数据源中的另一属性(OfType字符串)与组合框绑定到新的物业。这里的新楼盘的外观:

 公共只读属性myList中作为列表(串)
        得到
            昏暗cboxList作为新的列表(串)
            对于...
                cboxList.Add(新建字符串(...))
            下一个
            返回cboxList
        到底得的
    高端物业

XAML:

 < ListView控件IsSynchronizedWithCurrentItem =真保证金=11,5,0,0X:NAME =myList上的Horizo​​ntalAlignment =左WIDTH =130BorderBrush = {X:空}了borderThickness =0背景=白HEIGHT =240VerticalAlignment =评出的Grid.Column =1>
    < ListView.View>
    < GridView的AllowsColumnReorder =FALSE>
        < GridViewColumn标题=频率WIDTH =55>
        < GridViewColumn.CellTemplate>
            <&DataTemplate的GT;
            <组合框Horizo​​ntalContentAlignment =中心
                X:名称=_ ListFrequencies
                的ItemsSource ={结合TestStrings}
                的DisplayMemberPath =的TestString
                IsEnabled ={结合outCustomValue1,模式=双向,UpdateSourceTrigger =的PropertyChanged}
                的SelectionChanged =_ OnSelectionChanged
                的SelectedIndex ={结合outCustomValue2,模式=双向,UpdateSourceTrigger =的PropertyChanged}
                字号=10/>
                                    <! -
                                        < ComboBoxItem CONTENT =测试/>
                                        < ComboBoxItem CONTENT =测试/>
                                        < ComboBoxItem CONTENT =测试/>
                                         - >
            < / DataTemplate中>
        < /GridViewColumn.CellTemplate>
        < / GridViewColumn>
    < / GridView的>
    < /ListView.View>
< /&的ListView GT;

code-背后

 公共类MyComboBoxItems    公共财产TestStrings()方式列表(MyComboBoxStrings中)    公共职能MyComboBoxItems()
        TestStrings =新名单(MyComboBoxStrings中)
    结束功能末级公共类MyComboBoxStrings    公共只读属性TestString视为字符串
        得到
            返回测试
        到底得的
    高端物业末级


解决方案

  1. 您需要一个视图模型为您的方案。

相应的C#code(你可以使用它作为伪code):

 公共类视图模型
    {
        公开名单< MyComboBoxItems>项目{搞定;组; }
        公众诠释outCustomValue2 {搞定;组; }
        公共BOOL outCustomValue1 {搞定;组; }        公共视图模型()
        {
            项目=新的List< MyComboBoxItems>();
            items.Add(新MyComboBoxItems());
            items.Add(新MyComboBoxItems());
            items.Add(新MyComboBoxItems());
            items.Add(新MyComboBoxItems());
        }
    }    公共类MyComboBoxItems
    {
        公开名单< MyComboBoxStrings> TestStrings {搞定;组; }        公共MyComboBoxItems()
        {
            TestStrings =新的List< MyComboBoxStrings>();
            TestStrings.Add(新MyComboBoxStrings(val1中));
            TestStrings.Add(新MyComboBoxStrings(val1中));
            TestStrings.Add(新MyComboBoxStrings(val1中));
            TestStrings.Add(新MyComboBoxStrings(val1中));
        }
    }    公共类MyComboBoxStrings
    {
        串_testString;
        公共字符串的TestString {{返回_testString; }}        公共MyComboBoxStrings(字符串值)
        {
            _testString =价值;
        }
    }

适用的DataContext:

 视图模型VM =新视图模型();
 myList.DataContext = VM;

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).

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).

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?

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)

EDIT: 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>

Code-Behind

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. You need a ViewModel for your scenario.

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;
        }
    }

apply DataContext :

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

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

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