简体   繁体   中英

WPF: Custom collection ( List<Type>) databinding to a Listbox with Path filtering

I have created a custom DataSourceProvider that provides a List<XmlNode> as a Query result.

By using the Path parameter, the resulting Listbox should contain the specified list of the property named in XmlNode

Path=Tag should populate the listbox with the Tag(s) contained in the List. But instead it populates the list with the first List-items Tag.

The question is why the Path parameter not retreives all Tag elements from the List but only the letters from the first list elements Tag.



The Custom list is provided as a resource:

<Window.Resources>

    <classes:Custom_XmlProvider x:Key="txtProvider" Path="name" />

</Window.Resources>

The ListBox binds to the resource with the Path=Tag:

 <ListBox Grid.Row="1" ItemsSource="{Binding Path=Tag, Source={ StaticResource txtProvider}}"
             DockPanel.Dock="Bottom">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding}" FontFamily="Arial" FontWeight="Normal" Margin="5,0,0,0"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

The custom DataSourceProvider returns the List with 1200 objects:

protected override void BeginQuery()
    {
        System.Threading.ThreadPool.QueueUserWorkItem(RunQuery, null);
    }

    private void RunQuery(object state)
    {
        base.BeginQuery();

        try
        {
            //TODO - do something with the NodeList
            c_pData = c_thisXmlParser.NodeList;

            OnQueryFinished(c_pData, null, null, c_pData);
        }
        catch
        {
            OnQueryFinished(null, null, null, null);
        }
    }

The XmlNode is defined as:

 public class XmlNode
{
    public string Tag { get; set; }
    public string Type { get; set; }
    public string InnerText { get; set; }
    public int FileNumber { get; set; }
    public int NodeDepth { get; set; }
    public int IndexNumber { get; set; }
}

This is the first element in the list :

第一个清单项目

and the resulting list is shown as this in the window :

在此处输入图片说明

The above list should contain a list of Tag elements, and I cannot get a proper understanding of why the Path parameter does not collect the proper elements of the List, and rather provides a list of the first list-elements tag-name.

What you normally bind to ItemsSource is a property that contains some collection of elements. You have bound property Tag that is a string and a string happens to be a collection of characters. That is why each list item contains single character of the foodlist , and WPF is perfectly happy to interpret this binding as FirstOrDefault . What is wrong with your binding is the Path which should be empty. And then in your label where it is empty it should be Tag .

<ListBox Grid.Row="1" ItemsSource="{Binding, Source={ StaticResource txtProvider}}"
             DockPanel.Dock="Bottom">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding Path=Tag}" FontFamily="Arial" FontWeight="Normal" Margin="5,0,0,0"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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