none
WPF Binding multiple DataContexts

    Question

  • Hi! I have several columns in one Gridview and I have to bind two different objects to it. since I'm a beginner I took an example from the web.

     

    <Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
      <Grid>
        <StackPanel>
          <ListView Name="MyListview" ItemsSource="{Binding GameCollection}">
            <ListView.View>
              <GridView>
                <GridViewColumn Width="140" Header="Game Name" DisplayMemberBinding="{Binding GameName}" />
                <GridViewColumn Width="140" Header="Creator" DisplayMemberBinding="{Binding Creator}" />
                <GridViewColumn Width="140" Header="Publisher" DisplayMemberBinding="{Binding Publisher}" />
              </GridView>
            </ListView.View>
          </ListView>
          <Button HorizontalAlignment="Right" Margin="5,5,5,5" Content="Add Row" Click="AddRow_Click" />
        </StackPanel>
      </Grid>
    </Window>

     

    Basically I want to rewrite this code without {Binding RelativeSource={RelativeSource Self}} and add two Objects as DataSources to one grid

     

     

    Any help appreciated!

     

    Thank you very much!

    Mat

     

    Tuesday, May 31, 2011 6:49 PM

Answers

  • This isn't going to work with 2 collections, as you have described.  You'll need to make a class that "joins" these data elements into a single instance if you want to access members from both inside of a single data grid.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by MaGraBa Tuesday, May 31, 2011 7:55 PM
    Tuesday, May 31, 2011 7:17 PM

All replies

  • IF i remember correcly the:  DataContext="{Binding RelativeSource={RelativeSource Self}}"
    allows you to acsess all the propertuies in codebehind in XMAL.

    Not exactly sure but, If you have two properties you could simply remove  the line ItemsSource="{Binding GameCollection}" and add

       <GridViewColumn Width="140" Header="Game Name" DisplayMemberBinding="{Binding GameCollection.GameName}" />
                <GridViewColumn Width="140" Header="Creator" DisplayMemberBinding="{Binding GameCollection.Creator}" />
                <GridViewColumn Width="140" Header="Publisher" DisplayMemberBinding="{Binding GameCollection.Publisher}" />


    Kenneth
    Tuesday, May 31, 2011 7:01 PM
  • Thank you for your answer!

    If I understand you properly it would be like this ?!?!

    public class GameData                                                      public class User
      {                                                                                   {
        public string GameName { get; set; }                                public string Name { get; set; }
        public string Creator { get; set; }                                      public string Birthday { get; set; }
        public string Publisher { get; set; }                                   public string Birthplace { get; set; }
      }                                                                                    }

    ObservableCollection<GameData> _GameCollection =  new ObservableCollection<GameData>();
    ObservableCollection
    <User> _UserCollection =  new ObservableCollection<User>();

    public ObservableCollection<GameData> GameCollection  { get { return _GameCollection; } }
    public
    ObservableCollection<User> UserCollection  { get { return _UserCollection; } }

     

     

     
    <GridViewColumn Width="140" Header="Game Name" DisplayMemberBinding="{Binding GameCollection.GameName}" />
    
    <GridViewColumn Width="140" Header="Creator" DisplayMemberBinding="{Binding UserCollection.Name}" />
    <GridViewColumn Width="140" Header="Publisher" DisplayMemberBinding="{Binding GameCollection.Publisher}" />
    Tuesday, May 31, 2011 7:12 PM
  • This isn't going to work with 2 collections, as you have described.  You'll need to make a class that "joins" these data elements into a single instance if you want to access members from both inside of a single data grid.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by MaGraBa Tuesday, May 31, 2011 7:55 PM
    Tuesday, May 31, 2011 7:17 PM
  • Thank you for your answer Reed.

    So this means just one instance.

    My problem is that I parse one website (overview) and the URLs it contains are the websites for the detailsview. In my case performance is very important but the sites sometimes take al little to dl and parse. Right now each info is in a corresponding string.

    Maybe this is a stupid question but wth high latency is the grid updated when all the information is available or bit by bit. (I'm a beginner)

     

    Thank you!

     

    Mat

    Tuesday, May 31, 2011 7:30 PM
  • Mat,

     

    It depends on how you setup the objects.  In general, you usually populate the data, then display the grid, in which case you'll get high latency.  You could "pre-populate" the overview data, and have each item asynchronously load it's own data and "fill in" itself.  As long as the classes implement INotifyPropertyChanged appropriately, they'll appear to "fill in" as the data comes back from the service.

     

    -Reed

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, May 31, 2011 7:38 PM
  • Thank you for your quick answer.

    Yes my classes implement INotifyPropertyChange.

    First I wanted to use two grids but with sorting two grids simultanously/synchronously and my beginner skillset I just wanted to go with one.

    Hopefully I get it to work.

     

    Thank you for your help again!

     

    Mat

    Tuesday, May 31, 2011 7:55 PM