我想在列中使用带有列表框的Listview。 [英] I would like to use a Listview with listboxes in the columns.
问题描述
我正在为应用程序编写排序/过滤器控件。我想要的是使用列表视图来显示过滤器,并能够添加和删除过滤器中的行。我还是WPF的新手,所以我可以相信我犯了一些明显的错误,或者错过了其他一些简单的错误。
我的listview,因为我现在有它在XAML中
< ListView HorizontalAlignment =StretchMargin =5Name =lvFilterVerticalAlignment =Stretch>
< ListView.View>
< GridView>
< GridViewColumn Header =ColumnWidth =220>
< GridViewColumn.CellTemplate>
< DataTemplate>
< ListBox Name =lbColumnListItemsSource ={Binding Path = Name,Source = {StaticResource AvailableColumns}}/>
< / DataTemplate>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn Header =OperationWidth =130>
< GridViewColumn.CellTemplate>
< DataTemplate>
< ListBox Name =lbOperationItemsSource ={Binding Path = Value,Source = {StaticResource AvailableOperations}}/>
< / DataTemplate>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn Header =ValueWidth =250>
< GridViewColumn.CellTemplate>
< DataTemplate>
< TextBox Name =txtValue>< / TextBox>
< / DataTemplate>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< / GridView>
< /ListView.View>
< / ListView>
我希望有2个下拉菜单和一个文本框。每个列表框都将绑定到有效列表。在查看已保存的过滤器时,每个AND将在列中包含操作和值的行。成本> 1000.
我遗漏了一些关于如何在这个级别使用ListView的关键信息,我还没有找到任何关闭的例子。没有人有如何在模板中使用绑定列表框的示例。
任何帮助或示例都将非常感激。
我认为如果我理解你要构建的是什么,你的方法会稍微偏离。
本质上你想构建一个过滤器生成器是否正确?
所以你想用过滤器填充一个列表
这样的东西
列A | 运营商|价值|和/或
B栏| 运营商|价值|和/或
B栏| 运营商|价值|
如果是这种情况,我会使用更像这样的项目模板:
ComboBoxA | ComboBoxB |文本框| ComboboxC
ComboBoxA的数据源是可用列的列表
ComboBoxB的数据源是可用运算符的列表(可以根据所选列的数据类型
ComboBoxC为空/和/或(当它是最后一个选项时为空)然后只要选择And或Or,就可以添加主列表中的新条目。
这有意义吗?
------------ -------------------------------------------------- -
好的,所以这就是我制作的,这是一个非工作的例子,但它应该指向正确的方向。
这是一个要添加到Windows资源的DataTemplate:
< DataTemplate x:键 = filterEntry DataType = MyClasses:FilterComponent >
< 网格 >
< Grid.ColumnDefinitions >
< ColumnDefinition / >
< ColumnDefinition / >
< ColumnDefinition / >
< ColumnDefinition / >
< / Grid.ColumnDefinitions >
< ComboBox Grid.Column = 0 保证金 = 2 ItemsSource = {Binding Path = ColumnList} SelectedItem = {Binding Path = Column} / >
< span class =code-keyword>< ComboBox Grid.Column = 1 保证金 = 2 ItemsSource = {Binding Path = OperatorList} SelectedItem = {Binding Path =运营商} / >
< TextBox Grid.Column = 2 保证金 = 2 文字 = {Binding Path = FilterValue} / >
< ComboBox Grid.Column = 3 保证金 = 2 ItemsSource = {Binding Path = CombinerOperator.Combiner} SelectedItem = {Binding Path = Combiner} / >
< / Grid >
< / DataTemplate >
这个的目的是你设计的名为FilterComponent的类,它具有以下属性:
Column,Operator,FilterValue和Combiner,它们共同构成过滤器的一部分。
然后使用模板选择器/列表中的一个样式,当选择一行时应用此数据模板(如果视图模式不是编辑,则不显示所有控件)。
在视图模型中,您有一个属性,它是构成过滤器的FilterComponents的可观察集合。此可观察集合充当列表视图/列表框的项目源。
您还需要属性/枚举等作为数据模板上的项目源应用。
希望这有助于指明你正确的方向。 : - )
I am writing a sort/filter control for an application. What I would like is to use a list view to display the filter and to be able to add and remove lines from the filter. I'm still fairly new to WPF so I can believe I've made some obvious mistakes or missed something else that is simple.
my listview as I currently have it in XAML
<ListView HorizontalAlignment="Stretch" Margin="5" Name="lvFilter" VerticalAlignment="Stretch"> <ListView.View> <GridView> <GridViewColumn Header="Column" Width="220"> <GridViewColumn.CellTemplate> <DataTemplate> <ListBox Name="lbColumnList" ItemsSource="{Binding Path=Name, Source={StaticResource AvailableColumns}}"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Operation" Width="130"> <GridViewColumn.CellTemplate> <DataTemplate> <ListBox Name="lbOperation" ItemsSource="{Binding Path=Value, Source={StaticResource AvailableOperations}}"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Value" Width="250"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBox Name="txtValue"></TextBox> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView>
I'm looking to have 2 dropdowns and a textbox. Each of the listboxes will be bound to a valid list. When looking at a saved filter each 'AND' will have a line with the column the operation and the value ie. COST > 1000.
I'm missing some critical information on how to use the ListView at this level and I haven't found any examples even close. No one has an example of how to use a bound listbox in the template.
Any help or examples would be much appreciated.
I think your approach is slightly off if I am understanding what you are trying to build.
In essence you want to build a filter builder correct?
So you want to populate a list with the filters
so something like this
Column A | "operator" | value | And/Or
Column B | "operator" | value | And/Or
Column B | "operator" | value |
If that is the case, I would use an item template that is more like this:
ComboBoxA | ComboBoxB | Textbox | ComboboxC
ComboBoxA's datasource is a list of available columns
ComboBoxB's datasource is a list of available operators (which in turn can be filtered based on the datatype of the selected column
ComboBoxC is empty/and/or (empty for when it is the last option) then as soon as And or Or is selected a new entry in the main list can be added.
Does that make sense?
----------------------------------------------------------------
Okay, so this is what I produced, it is a non-working example but it should point you in the right direction.
This is a DataTemplate to be added to your windows resources:
<DataTemplate x:Key="filterEntry" DataType="MyClasses:FilterComponent"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <ComboBox Grid.Column="0" Margin="2" ItemsSource="{Binding Path=ColumnList}" SelectedItem="{Binding Path=Column}" /> <ComboBox Grid.Column="1" Margin="2" ItemsSource="{Binding Path=OperatorList}" SelectedItem="{Binding Path=Operator}" /> <TextBox Grid.Column="2" Margin="2" Text="{Binding Path=FilterValue}" /> <ComboBox Grid.Column="3" Margin="2" ItemsSource="{Binding Path=CombinerOperator.Combiner}" SelectedItem="{Binding Path=Combiner}" /> </Grid> </DataTemplate>
The purpose of this is you designed a class called "FilterComponent" which has properties:
Column, Operator, FilterValue and Combiner, which together make up a section of the filter.
You then use a template selector / a style on the list which applies this data template when a row is selected (no point having all the controls displayed if it is view mode not edit).
On your view model, you have a property that is an observable collection of "FilterComponents" which make up your filter. This observable collection acts as the items source for the listview/listbox.
You also need properties/enums etc to apply as the items sources on the datatemplate.
Hopefully that should help point you in the right direction. :-)
这篇关于我想在列中使用带有列表框的Listview。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!