使用文本框筛选 绑定到 xml 的列表视图
本文关键字:xml 列表 视图 绑定 文本 筛选 | 更新日期: 2023-09-27 17:56:17
enter code here
嘿伙计们,我是 wpf 编程的新手,当用户在文本框中键入内容时,我很难过滤列表视图。 当尝试过滤时,我得到的只是一个黑名单视图......我相信这是由于我过滤的部分是节点的属性引起的,我不确定如何使用它来过滤
例我正在尝试使用项目的基本参考进行过滤
提前致谢
这是我的代码
private bool FilterItem(object obj)
{
Item item = obj as Item;
if (item == null) return false;
string textFilter = Filtertxt.Text;
if (textFilter.Trim().Length == 0) return true; // the filter is empty - pass all items
// apply the filter
if (item.ItemNum.ToLower().Contains(textFilter.ToLower())) return true;
return false;
}
private void Filtertxt_TextChanged(object sender, TextChangedEventArgs e)
{
if (!this.IsInitialized) return; // get out of here if the window is not initialized
/// Get the default view from the listview
ICollectionView view = CollectionViewSource.GetDefaultView(lviewItemsList.ItemsSource );
view.Filter = null;
view.Filter = new Predicate<object>(FilterItem);
}
My Xml struct
<Item quantity="0" baseRef="2" desc="BLANK Item, ">
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
</Item>
<Item quantity="0" baseRef="2" desc="BLANK Item, ">
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
<Material selected="0" ref="2" color="" />
</Item>
使用 xaml 中的 xpath 解析 XML
<ListView Name="lviewItemsList"
MouseDoubleClick="DataGrid_MouseDoubleClick"
ItemsSource="{Binding Source={StaticResource plData}, XPath=/PickList/Items/Item}"
IsSynchronizedWithCurrentItem="True" Margin="87,32,63.9999999999991,61"
KeyboardNavigation.TabNavigation="Continue" Grid.Row="1" >
<ListView.View>
<GridView x:Name="Itemlist" ColumnHeaderContainerStyle="{StaticResource GridHeaderStyle}">
<GridViewColumn Header="Item" DisplayMemberBinding="{Binding XPath=@baseRef}"/>
<GridViewColumn Header ="Qty" CellTemplate="{StaticResource Qty}"/>
<GridViewColumn Header="Finish" CellTemplate="{StaticResource ItemFin}" Width="0"/>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding XPath=@desc}"/>
</GridView>
</ListView.View>
</ListView>
enter code here
ItemNum是一个项目类..我试图作为一个对象传递..我真的迷失在这里
internal class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void FirePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string itemNum = "";
public string ItemNum
{
get { return itemNum; }
set { itemNum = value; FirePropertyChanged("ItemNum"); }
}
private string itemDesc = "";
public string ItemDesc
{
get { return itemDesc; }
set { itemDesc = value; FirePropertyChanged("ItemDesc "); }
}
internal class ItemCollection : ObservableCollection<Item>
{
}
}
}
似乎您直接绑定到包含名为 Item
的元素的XmlDocument
,然后尝试将其过滤,就像您定义的名为 Item
的类一样。这是不对的,你的FilterItem()
收到的对象只是一个XmlElement
。框架应该如何确定您要将其转换为恰好具有相同名称的某个类?(如果它设法弄清楚你想要的,它如何进行转换?