views:

167

answers:

2

Hello,

Basically I have a WPF application that will display announcements to the user within a listbox. The information that populates the list box comes from a XML file which contains details about different announcements. I am then applying a data template to display the relevant pieces of information. What I have is two buttons at the bottom of the application, what I want is when the user clicks on the unread announcements button that the data is filtered to only show the unread announcements in the listbox. Then when the user clicks on All announcements I want them to all be shown again. The data template that I'm using will remain the same whichever button is clicked.

In the XML file which is being used there is a field called read, with 0 indicating it hasn't been read while 1 indicates that it has and this should be used to filter.

So really I'm just looking for examples of filtering or any good tutorials, so I can make some progress. If you require any of my code just let me know and I'll paste it in.

EDIT

Now added code below to show how the data is bound and also the layout of the data:

<XmlDataProvider
                        x:Key="Article"
                        Source="/Data/People.xml"
                        XPath="People/article"/>
<DataTemplate x:Key="AnnouncementTemplate">
            <Border Background="#2200FF00" 
                            BorderBrush="Black" 
                            BorderThickness="1" 
                            CornerRadius="8"  
                            Margin="2,4,2,4" 
                            Padding="2"
                    >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition Width="220"/>
                    </Grid.ColumnDefinitions>
                    <Border Background="#2200FF00" 
                            BorderBrush="Black" 
                            BorderThickness="1" 
                            CornerRadius="8"  
                            Margin="2,4,2,4" 
                            Padding="2"
                    >
                    <StackPanel Grid.Column="0">
                        <Image Stretch="Fill" Width="20" Source="Images/BlueOrb.ico" Height="20" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
                <StackPanel Grid.Column="1" Margin="5">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontWeight="Bold" Text="{Binding XPath=./author}" />
                        <TextBlock Text=" "/>
                        <TextBlock Text="{Binding XPath=./datetime}" TextTrimming="WordEllipsis"/>
                    </StackPanel>
                    <StackPanel>
                        <TextBlock FontWeight="Bold" Text="{Binding XPath=./title}"/>
                    </StackPanel>
                    <StackPanel>
                        <TextBlock Text="{Binding XPath=./body}" TextTrimming="WordEllipsis"/>
                    </StackPanel>
                 </StackPanel>

                </Grid>
            </Border>
        </DataTemplate>

<ListBox Style="{StaticResource SpecialListStyle}"
                         Name="listBox1"
                         Margin="10,10,10,10"
                         Grid.Row="1"
                         ItemsSource="{Binding Source={StaticResource Article}}" 
                         IsSynchronizedWithCurrentItem="True"
                         ItemContainerStyle="{StaticResource SpecialListItem}"
                         ItemTemplate="{StaticResource AnnouncementTemplate}"/>

Example of the data held in the XML file, the data wants to be filtered so it only shows messages where read == 0

<People>
  <article>
    <author>Michael</author>
    <title>Happy Christmas</title>
    <body>Merry Christmas one and all</body>
    <priority>1</priority>
    <uid>G-0617-8567-EF32-hello</uid>
    <audience>0</audience>
    <read>0</read>
    <datetime>December 31, 2009 23:59:59</datetime>
  </article>
  <article>
    <author>Gary</author>
    <title>Landesk</title>
    <body>Landesk Distributed</body>
    <priority>0</priority>
    <uid>G-0618-8567-EF32-hello</uid>
    <audience>0</audience>
    <read>1</read>
    <datetime>December 31, 2009 23:59:59</datetime>
  </article>
</People>
+1  A: 

When you bind your data to a listbox or any similar control, WPF wraps it with CollectionViewSource instance.

So let's say your data source is named as myItems. Then you can get this instance as follows:

ICollectionViewSource viewSource = CollectionViewSource.GetDefaultView(myItems);

ICollectionViewSource has a Filter property that you can use to filter items. You can also force visual control to refresh its content by using ICollectionViewSource.Refresh().

idursun
A: 

I've now been able to resolve this using the following code which also involved making the read node of the XML file into an attribute of the article element:

ICollectionView RefineList()
    {
        DataSourceProvider provider = (DataSourceProvider)this.FindResource("Article");
        return CollectionViewSource.GetDefaultView(provider.Data);
    }

        private void Unread_Click(object sender, RoutedEventArgs e)
    {
        ICollectionView view = RefineList();
         if (view.Filter == null)
        {
            view.Filter = delegate(object item)
            {
                return
                int.Parse(((XmlElement)item).Attributes["read"].Value) == 0;
            };
        }
        else
        {
            view.Filter = null;
        }   
    }
manemawanna