开发者

Using a Text Box To Filter A listview that is bound to a xml

开发者 https://www.devze.com 2023-03-05 14:01 出处:网络
enter code hereHey Guys, I am new to the wpf programming and I am having a hard time filter the list view when the user types in the text box.when trying to filter all i get is a black listview ... I

enter code hereHey Guys, I am new to the wpf programming and I am having a hard time filter the list view when the user types in the text box. when trying to filter all i get is a black listview ... I believe this is caused from the the part that I am filter is an attribute of the Node and I am not sure how to use that to filter

Example i am trying to filter using the baseRef of Item

Thanks in advance

Here is my code

    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>

Xml is parsed using xpath in the xaml

<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

the ItemNum is an item class .. i tried to pass as an object.. I am really lost here

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

}


It seems you are binding directly to a XmlDocument that contains an element called Item and then try to filter it like it was a class you defined called Item. That's not right, the object your FilterItem() receives is just an XmlElement. How should the framework figure out that you want to convert it to some class that happens to have the same name? (And how it do the conversion if it managed to figure out you want that?)

0

精彩评论

暂无评论...
验证码 换一张
取 消