Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. Listview binding is not working with page class

Listview binding is not working with page class

Scheduled Pinned Locked Moved WPF
wpfcssdotnetwcfcom
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L laprathab

    Hi, I tried to bind the list view with combo box, following code worked fine with Window1.xaml the same was not working in Page1.xaml. Any one pls help to solve the above. Thanks in advance. My Xaml Code is:

    <Page x:Class="WPFtest.ComboTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPFtest"
    Title="ComboTest">
    <Page.Resources>
    <local:BoolToVisibilityConverter x:Key="boolToVis" />

        <Style TargetType="{x:Type TextBlock}" 
           x:Key="GridBlockStyle">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Visibility" 
              Value="{Binding Path=IsSelected, 
              RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type ListViewItem}},
              Converter={StaticResource boolToVis}, 
                         ConverterParameter=False}" />
        </Style>
    
        <Style TargetType="{x:Type FrameworkElement}" 
           x:Key="GridEditStyle">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Visibility" 
              Value="{Binding Path=IsSelected, 
              RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type ListViewItem}},
              Converter={StaticResource boolToVis}, 
                         ConverterParameter=True}" />
        </Style>
    
    </Page.Resources>
    <StackPanel>
        <TextBox Height="23" Name="textBox1" Width="120" />
        <Button Height="23" Name="btnTest" Content="test" Width="120" Click="btnTest\_Click"></Button>
        <ListView x:Name="gameListView" ItemsSource="{Binding Path=GameCollection}">
            <ListView.View>
                <GridView>
    
                    <GridViewColumn Width="140">
                        <GridViewColumnHeader Click="SortClick" 
                                  Tag="GameName" 
                                  Content="Game Name" />
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <TextBlock Text="{Binding Path=GameName}"  Style="{StaticResource GridBlockStyle}"/>
    
    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    What does not work?

    Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

    Just a grain of sand on the worlds beaches.

    L 1 Reply Last reply
    0
    • L Lost User

      What does not work?

      Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

      Just a grain of sand on the worlds beaches.

      L Offline
      L Offline
      laprathab
      wrote on last edited by
      #3

      Hi Karl, Page loading with empty listview items from the ObservableCollections.I hv posted my code behind for your reference.

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Data;
      using System.Windows.Documents;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Windows.Media.Imaging;
      using System.Windows.Navigation;
      using System.Windows.Shapes;
      using System.Collections.ObjectModel;
      using System.ComponentModel;

      namespace WPFtest
      {
      /// <summary>
      /// Interaction logic for ComboTest.xaml
      /// </summary>
      public partial class ComboTest : Page
      {

              private ObservableCollection<GameData> \_GameCollection =  new ObservableCollection<GameData>();
              private ObservableCollection<Department> \_AvailablePublishers = new ObservableCollection<Department>();
              private ObservableCollection<Role> \_AvailableRoles = new ObservableCollection<Role>();
              private GridViewColumnHeader \_CurSortCol = null;
              private SortAdorner \_CurAdorner = null;
      
      public ComboTest()
      {
        \_GameCollection.Add(new GameData { 
          GameName = "World Of Warcraft", 
          Creator = "Blizzard", 
          Publisher = "Blizzard" });
        \_GameCollection.Add(new GameData { 
          GameName = "Halo", 
          Creator = "Bungie", 
          Publisher = "Microsoft" });
        \_GameCollection.Add(new GameData { 
          GameName = "Gears Of War", 
          Creator = "Epic", 
          Publisher = "Microsoft" });
      
      
        \_AvailablePublishers.Add(new Department { DeptID = "1", DeptName = "Purchase" });
        \_AvailablePublishers.Add(new Department { DeptID = "2", DeptName = "production" });
        \_AvailablePublishers.Add(new Department { DeptID = "3", DeptName = "Inventory" });    
        //\_AvailablePublishers.Add("Microsoft");
        //\_AvailablePublishers.Add("Blizzard");
        //\_AvailablePublishers.Add("Nintendo");
        //\_AvailablePublishers.Add("Electronic Arts");
        //\_AvailablePublishers.Add("Activision");
        //\_AvailablePublishers.Add("Ubisoft");
        //\_AvailablePublishers.Add("Take-Two Interactive");
      
        InitializeComponent();
      }
      
      public ObservableCollection<GameData> GameCollection
      { get { return \_GameCollection; } }
      
      public ObservableCollection<Department> AvailablePublishers
      { get {
      
      L 1 Reply Last reply
      0
      • L laprathab

        Hi Karl, Page loading with empty listview items from the ObservableCollections.I hv posted my code behind for your reference.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Windows;
        using System.Windows.Controls;
        using System.Windows.Data;
        using System.Windows.Documents;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Windows.Media.Imaging;
        using System.Windows.Navigation;
        using System.Windows.Shapes;
        using System.Collections.ObjectModel;
        using System.ComponentModel;

        namespace WPFtest
        {
        /// <summary>
        /// Interaction logic for ComboTest.xaml
        /// </summary>
        public partial class ComboTest : Page
        {

                private ObservableCollection<GameData> \_GameCollection =  new ObservableCollection<GameData>();
                private ObservableCollection<Department> \_AvailablePublishers = new ObservableCollection<Department>();
                private ObservableCollection<Role> \_AvailableRoles = new ObservableCollection<Role>();
                private GridViewColumnHeader \_CurSortCol = null;
                private SortAdorner \_CurAdorner = null;
        
        public ComboTest()
        {
          \_GameCollection.Add(new GameData { 
            GameName = "World Of Warcraft", 
            Creator = "Blizzard", 
            Publisher = "Blizzard" });
          \_GameCollection.Add(new GameData { 
            GameName = "Halo", 
            Creator = "Bungie", 
            Publisher = "Microsoft" });
          \_GameCollection.Add(new GameData { 
            GameName = "Gears Of War", 
            Creator = "Epic", 
            Publisher = "Microsoft" });
        
        
          \_AvailablePublishers.Add(new Department { DeptID = "1", DeptName = "Purchase" });
          \_AvailablePublishers.Add(new Department { DeptID = "2", DeptName = "production" });
          \_AvailablePublishers.Add(new Department { DeptID = "3", DeptName = "Inventory" });    
          //\_AvailablePublishers.Add("Microsoft");
          //\_AvailablePublishers.Add("Blizzard");
          //\_AvailablePublishers.Add("Nintendo");
          //\_AvailablePublishers.Add("Electronic Arts");
          //\_AvailablePublishers.Add("Activision");
          //\_AvailablePublishers.Add("Ubisoft");
          //\_AvailablePublishers.Add("Take-Two Interactive");
        
          InitializeComponent();
        }
        
        public ObservableCollection<GameData> GameCollection
        { get { return \_GameCollection; } }
        
        public ObservableCollection<Department> AvailablePublishers
        { get {
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        I don't see where you have assigned the DataContext. You must set the ComboTest DataContext to the ComboTest class. In you Loaded event add this line of code: this.DataContext = this;

        Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

        Just a grain of sand on the worlds beaches.

        L 1 Reply Last reply
        0
        • L Lost User

          I don't see where you have assigned the DataContext. You must set the ComboTest DataContext to the ComboTest class. In you Loaded event add this line of code: this.DataContext = this;

          Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

          Just a grain of sand on the worlds beaches.

          L Offline
          L Offline
          laprathab
          wrote on last edited by
          #5

          Hi Karl, Thanks...now listview loaded with data. I have one more issue, combo box not loaded.pls give me some suggestions. Thanks inadvance

          L 1 Reply Last reply
          0
          • L laprathab

            Hi Karl, Thanks...now listview loaded with data. I have one more issue, combo box not loaded.pls give me some suggestions. Thanks inadvance

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            The ItemsSource is incorrect.

            ItemsSource="{Binding ElementName=This

            Change to ItemsSource={Binding Path=...} ... is the property name that holds the collection.

            Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

            Just a grain of sand on the worlds beaches.

            L 1 Reply Last reply
            0
            • L Lost User

              The ItemsSource is incorrect.

              ItemsSource="{Binding ElementName=This

              Change to ItemsSource={Binding Path=...} ... is the property name that holds the collection.

              Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

              Just a grain of sand on the worlds beaches.

              L Offline
              L Offline
              laprathab
              wrote on last edited by
              #7

              Hi, till combo is not loaded,

              <ComboBox Name="cmbTest" SelectedItem="{Binding Path=DeptID}" DisplayMemberPath="DeptName" SelectedValuePath="DeptID" ItemsSource="{Binding ElementName=This,Path=AvailablePublishers}" Style="{StaticResource GridEditStyle}" SelectionChanged="ComboBox_SelectionChanged" />

              L 1 Reply Last reply
              0
              • L laprathab

                Hi, till combo is not loaded,

                <ComboBox Name="cmbTest" SelectedItem="{Binding Path=DeptID}" DisplayMemberPath="DeptName" SelectedValuePath="DeptID" ItemsSource="{Binding ElementName=This,Path=AvailablePublishers}" Style="{StaticResource GridEditStyle}" SelectionChanged="ComboBox_SelectionChanged" />

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                laprathab wrote:

                ItemsSource="{Binding ElementName=This,Path=AvailablePublishers}"

                This is wrong. Change to: ItemsSource="{Binding Path=AvailablePublishers}"

                Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

                Just a grain of sand on the worlds beaches.

                L 1 Reply Last reply
                0
                • L Lost User

                  laprathab wrote:

                  ItemsSource="{Binding ElementName=This,Path=AvailablePublishers}"

                  This is wrong. Change to: ItemsSource="{Binding Path=AvailablePublishers}"

                  Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

                  Just a grain of sand on the worlds beaches.

                  L Offline
                  L Offline
                  laprathab
                  wrote on last edited by
                  #9

                  I hv changed but it was not working.

                  L 1 Reply Last reply
                  0
                  • L laprathab

                    I hv changed but it was not working.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    I looked at your code again, the ComboBox can't see the Page DataContext. There are many ways around this issue. Two simple solutions are; 1. Binding the ItemsSource in code. 2. Add a CollectionViewSource to the Page.Resources section, then bind the ComboBox to the CollectionViewSource. Assign the soure of the CollectionViewSource to your AvailablePublishers. If you use this solution you must set the ComboBox property, IsSynchronizedWithCurrentItem to False since you are binding two ComboBoxes to the same source.

                    Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

                    Just a grain of sand on the worlds beaches.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      I looked at your code again, the ComboBox can't see the Page DataContext. There are many ways around this issue. Two simple solutions are; 1. Binding the ItemsSource in code. 2. Add a CollectionViewSource to the Page.Resources section, then bind the ComboBox to the CollectionViewSource. Assign the soure of the CollectionViewSource to your AvailablePublishers. If you use this solution you must set the ComboBox property, IsSynchronizedWithCurrentItem to False since you are binding two ComboBoxes to the same source.

                      Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

                      Just a grain of sand on the worlds beaches.

                      L Offline
                      L Offline
                      laprathab
                      wrote on last edited by
                      #11

                      Thanks very much...I tried 2nd solution that works fine...

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups