简体   繁体   English

组合框和列表视图绑定问题WPF C#

[英]combobox and listview binding issue wpf c#

I have a combobox which displays listview on dropdown, I am following MVVM Pattern and i have also set the public property in my Viewmodel and it works fine when i am assigning it to the Label but for Combobox it doesn't seem to rely on my binding. 我有一个组合框,在下拉菜单中显示列表视图,我在跟随MVVM Pattern,并且还在Viewmodel中设置了public属性,当我将其分配给Label时,它可以正常工作,但是对于Combobox,它似乎并不依赖于我捆绑。 i tried numerous ways but unable to find the issue. 我尝试了很多方法,但是找不到问题。

XAML : XAML:

 <ComboBox Name="SearchBox" IsEditable="True"  Background="White"  md:HintAssist.Hint="Search MUID"    Grid.Column="1" Margin="5 0 0 0" 
                  Grid.Row="0"  Height="40" Width="400"  HorizontalContentAlignment="Left" HorizontalAlignment="Left" SelectedItem="{Binding ElementName=lstview ,Path=SelectedItem}" >
                                <ComboBoxItem>
                                <ListView x:Name="lstview" ItemsSource="{Binding filterSW}" 
                    SelectedItem="{Binding SelectedMU}" 
                     Height="200" ScrollViewer.VerticalScrollBarVisibility="Visible">
                                    <ListView.View>
                                        <GridView>
                                            <GridViewColumn Width="130"  Header="Mu-ID"    />
                                            <GridViewColumn Width="130" Header="MU-Identifier" DisplayMemberBinding="{Binding MU_Identifier}"  />
                                            <GridViewColumn Width="130"  Header="Status" DisplayMemberBinding="{Binding RequestType}" />
                                            <GridViewColumn Width="130" Header="UniqueID"  />
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                                </ComboBoxItem>
                            </ComboBox>

This works fine for me when i am using the public property and accessing its element , i also tried setting text={Binding SelectedMU.MU_Identifier} and selectedvalue but its just not working. 当我使用公共属性并访问其元素时,这对我来说效果很好,我也尝试设置text = {Binding SelectedMU.MU_Identifier}和selectedvalue,但它却无法正常工作。

 <Label Grid.Column="3" HorizontalAlignment="Center"  Background="GreenYellow" Content="{Binding SelectedMU.MU_Identifier}"></Label>

It looks like you're trying to show a multi-column list in your ComboBox dropdown instead of the standard list where each item shows just a text line. 看起来您正在尝试在ComboBox下拉列表中显示多列列表,而不是在标准列表中每个项目仅显示一个文本行。

To achieve this effect you've placed a ListView inside the dropdown. 为了达到这种效果,您已经在下拉列表中放置了一个ListView

Unfortunately, this is just not going to work. 不幸的是,这只是行不通的。

Both ComboBox and ListView descend from Selector which is an abstraction that allows to select an item from a list. ComboBoxListViewSelector继承, Selector是允许从列表中选择项目的抽象。 This limits the property SelectedItem to one of the items that are contained in the list . 这将属性SelectedItem限制为列表中包含的项目之一。 If you try to assign to this property any value that it not in the list, the assignment is not going to work and the property will retain the value it had before you did the assignment. 如果您尝试为该属性分配列表中未包含的任何值,则该赋值将无法使用,并且该属性将保留执行赋值之前的值。

Now, the list could either be specified right inside XAML or provided as a binding to property ItemsSource . 现在,可以在XAML内部指定该列表,也可以将其作为对属性ItemsSource的绑定提供。 You do the binding correctly for the ListView . 您正确地为ListView绑定。 But for the ComboBox you don't specify that binding. 但是对于ComboBox您无需指定该绑定。 Instead you specify exactly one item of type ComboBoxItem which contains the whole ListBox as its value. 相反,您只需要指定一个ComboBoxItem类型的项,其中包含整个ListBox作为其值。 So the only value that could be successfully assigned to the SelectedItem property of the ComboBox is that single ComboBoxItem . 因此,唯一可以成功分配给ComboBoxSelectedItem属性的值是单个ComboBoxItem But your binding is never going to assign that value, that's why the ComboBox never shows anything when closed. 但是您的绑定永远不会分配该值,这就是ComboBox在关闭时永远不会显示任何内容的原因。

When it's open it does show the single item which contain the ListView but this is just an optical effect. 当它打开时,它确实会显示包含ListView的单个项目,但这只是一种光学效果。 The data binging is not going to work. 数据绑定无法正常工作。 The reason why it works for the Label is because the Label is not constrained and can show anything that the ListView tells it to show. 它对Label起作用的原因是Label不受约束,并且可以显示ListView告诉其显示的任何内容。

You can synchronize the ListView and the ComboBox only when both controls have the same bindings for both ItemsSource and SelectedItem properties. 仅当两个控件对ItemsSourceSelectedItem属性具有相同的绑定时,才可以同步ListViewComboBox But in this case you won't be able to place the ListView inside the dropdown. 但在这种情况下,你将不能够到的地方ListView下拉

The closest you can get to what you want is by customizing the ComboBox 's template as described in https://zamjad.wordpress.com/2012/08/15/multi-columns-combo-box , for example. 例如,通过自定义ComboBox的模板,可以最接近所需的内容,如https://zamjad.wordpress.com/2012/08/15/multi-columns-combo-box中所述。 What this won't give you compared to ListView is the column headers. ListView相比,这不会给您带来的是列标题。 Also, the columns will be evenly spaced inside the dropdown but this is what you have in your ListView anyway. 同样,列在下拉列表中将均匀分布,但这仍然是ListView的内容。

If you want to auto-size them, you'd need to add Width="Auto" SharedSizeGroup="cN" to each ColumnDefinition where "cN" should have the column number instead of N to make them unique within the Grid and add Grid.IsSharedSizeScope="True" to the <ComboBox > 如果要自动调整它们的大小,则需要为每个ColumnDefinition添加Width="Auto" SharedSizeGroup="cN" ,其中“ cN”应该具有列号而不是N,以使其在Grid唯一,并添加Grid.IsSharedSizeScope="True"<ComboBox >

That's a lot of trouble for something that one would expect to be much simpler, but, unfortunately, you cannot place a ListView inside the ComboBox 's template, that's a limitation of how the base class Selector works with its items list. 对于某些人希望它更简单的事情来说,这很麻烦,但是不幸的是,您不能在ComboBox的模板中放置ListView ,这限制了基类Selector与其项目列表一起工作的方式。

There are other options if you are open to consider 3rd party control libraries. 如果您愿意考虑使用第三方控制库,还有其他选择。 I worked with Syncfusion , they have SfMultiColumnDropDown which does what you want. 我使用Syncfusion进行工作 ,它们具有SfMultiColumnDropDown来满足您的需求。 I'm pretty sure other popular libraries have similar controls as well. 我很确定其他流行的库也有类似的控件。

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

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