0

I have a custom object that has a handful of properties and a ObservableCollection.

The problem I'm having is getting them to all play nicely. It seems I'd like a CollectionViewSource to filter but I cant figure out how that applies so that I can run the filter on the code side.

I havent been able to find a good example of binding like this that takes in to account multiple things (proeprties on the main object, a collection with a filter)

class Student
{
    public string Name { get; set; }
    public DateTime DOB { get; set; }
    public ObservableCollection<ClassRoom> Classes { get; set; }

    public Student()
    {
        this.Classes = new ObservableCollection<ClassRoom>();
    }
}

class ClassRoom
{
    public string Name { get; set; }
    public int Room { get; set; }
}

using that, i do this in the main.cs

var student = new Student { Name = "Justin", DOB = new DateTime(1983, 6, 15) };
student.Classes.Add(new ClassRoom { Name = "math", Room = 102});
student.Classes.Add(new ClassRoom { Name = "english", Room = 119 });
this.DataContext = student;

so i can then do this in xaml

<TextBlock Grid.Row="0" Grid.Column="0">
        <Run Text="Name: "/>
        <Run Text="{Binding Name}"/>
    </TextBlock>
    <TextBlock Grid.Row="0" Grid.Column="1">
        <Run Text="DOB: "/>
        <Run Text="{Binding Name}"/>
    </TextBlock>
    <DataGrid Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0" 
        ItemsSource="{Binding Classes}" AutoGenerateColumns="True"></DataGrid>

I really just want to create the CVS against the Classes property, so that way i can assign the CSV to the DataGrid and deal with custom column bindings with just Binding Room rather then Binding Classes.Room

at least i suspect, basically the issue is i couldnt find any example/tutorials that talk about mixing a OC with other data.

jrich523
  • 598
  • 1
  • 5
  • 19

1 Answers1

0

To cut the story short, CollectionViewSource allows you to have a "virtual view" over your source that allows you to group or sort data in the "view" while source still stays unchanged.

Controls in wpf which know how to deal with this do not use source but instead bind to the view and so when you sort in your CollectionViewSource object the control is following you and changing its displayed content the way you want to.

Though since you havent posted much of code and havent specified any exact issue I will gladly answer you as broadly as you asked your question.

Here are links that will help you understand grouping, sorting, filtering in wpf:

http://msdn.microsoft.com/en-us/library/ff407126.aspx

http://wpftutorial.net/DataViews.html

Check them out, try it yourself. Once you get stuck somewhere and google doesnt seem to be helping you any futher feel free to post a question here with code example.

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55
  • Thanks, both of those links speak exactly to what my problem is. they both assume all im sending is the collection itself. i'll update the question with some code – jrich523 Oct 23 '13 at 15:30