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
  1. Home
  2. General Programming
  3. WCF and WF
  4. How do you access / databind a Global ObservableCollection<> in XAML

How do you access / databind a Global ObservableCollection<> in XAML

Scheduled Pinned Locked Moved WCF and WF
wpfquestionwcfarchitecturehelp
4 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.
  • S Offline
    S Offline
    Sevententh
    wrote on last edited by
    #1

    Hi all, I'm not sure that the heading is right, but I'll explain the problem and hopefully somebody can point me in the right direction. And I know this is slightly long but I'm trying to give lots of info. Note: I'm using the MVVM concept I have a class ComboLookupViewModel which contains a few public ObservableCollections i.e List of Time Zones and Currencies.

    public class ComboLookupViewModel
    {
    private ObservableCollection<tblCfgTimeZones> currentTimeZone = new ObservableCollection<tblCfgTimeZones>();

        public ComboLookupViewModel()
        {
            GetList\_TimeZones();
        }
    
        public ObservableCollection<tblCfgTimeZones> CurrentTimeZone
        {
            get { return currentTimeZone; }
            set { currentTimeZone = value; }
        }
    
        private void GetList\_TimeZones()
        {
            currentTimeZone = new ObservableCollection<tblCfgTimeZones>(svc.FindTimeZoneList());
        }
    }
    

    In the code behind of App class I have:

    public partial class App : Application
    {
    public static ComboLookupViewModel comboboxLookups = new ComboLookupViewModel();
    }

    This all works fine and the data is loaded. But I would like to view this list. Question: How do you do this, as the following databinding does not display anything (within a UserControl)?

    <ListView x:Name="lstTimeZones"
    ItemsSource="{Binding Path=App.comboboxLookups.CurrentTimeZone}"
    ScrollViewer.CanContentScroll="True"
    SelectionMode="Single"
    SelectionChanged="lstTimeZones_SelectionChanged"
    Background="Transparent" BorderBrush="Transparent">
    <ListView.View>
    <GridView>
    <GridViewColumn Header="UTC Name" Width="120" DisplayMemberBinding="{Binding Path=UTCName}"/>
    <GridViewColumn Header="UTC Value" Width="120" DisplayMemberBinding="{Binding Path=UTCValue}"/>
    </GridView>
    </ListView.View>
    </ListView>

    Currently my work around is to use another ObservableCollection in the ViewModel for the UserControl

    public class TimeZoneViewModel
    {
    public ObservableCollection<tblCfgTimeZones> listTimeZone = new ObservableCollection<tblCfgTimeZones>();

        public TimeZoneViewModel()
        {
            listTimeZone = new ObservableCollection<tblCfgTimeZones>(App.combobo
    
    A 1 Reply Last reply
    0
    • S Sevententh

      Hi all, I'm not sure that the heading is right, but I'll explain the problem and hopefully somebody can point me in the right direction. And I know this is slightly long but I'm trying to give lots of info. Note: I'm using the MVVM concept I have a class ComboLookupViewModel which contains a few public ObservableCollections i.e List of Time Zones and Currencies.

      public class ComboLookupViewModel
      {
      private ObservableCollection<tblCfgTimeZones> currentTimeZone = new ObservableCollection<tblCfgTimeZones>();

          public ComboLookupViewModel()
          {
              GetList\_TimeZones();
          }
      
          public ObservableCollection<tblCfgTimeZones> CurrentTimeZone
          {
              get { return currentTimeZone; }
              set { currentTimeZone = value; }
          }
      
          private void GetList\_TimeZones()
          {
              currentTimeZone = new ObservableCollection<tblCfgTimeZones>(svc.FindTimeZoneList());
          }
      }
      

      In the code behind of App class I have:

      public partial class App : Application
      {
      public static ComboLookupViewModel comboboxLookups = new ComboLookupViewModel();
      }

      This all works fine and the data is loaded. But I would like to view this list. Question: How do you do this, as the following databinding does not display anything (within a UserControl)?

      <ListView x:Name="lstTimeZones"
      ItemsSource="{Binding Path=App.comboboxLookups.CurrentTimeZone}"
      ScrollViewer.CanContentScroll="True"
      SelectionMode="Single"
      SelectionChanged="lstTimeZones_SelectionChanged"
      Background="Transparent" BorderBrush="Transparent">
      <ListView.View>
      <GridView>
      <GridViewColumn Header="UTC Name" Width="120" DisplayMemberBinding="{Binding Path=UTCName}"/>
      <GridViewColumn Header="UTC Value" Width="120" DisplayMemberBinding="{Binding Path=UTCValue}"/>
      </GridView>
      </ListView.View>
      </ListView>

      Currently my work around is to use another ObservableCollection in the ViewModel for the UserControl

      public class TimeZoneViewModel
      {
      public ObservableCollection<tblCfgTimeZones> listTimeZone = new ObservableCollection<tblCfgTimeZones>();

          public TimeZoneViewModel()
          {
              listTimeZone = new ObservableCollection<tblCfgTimeZones>(App.combobo
      
      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      Have a look at this Binding to global App properties article[^] by Sacha Barber.

      S 1 Reply Last reply
      0
      • A ABitSmart

        Have a look at this Binding to global App properties article[^] by Sacha Barber.

        S Offline
        S Offline
        Sevententh
        wrote on last edited by
        #3

        Thanks... this is very close to what I needed :-D

        S 1 Reply Last reply
        0
        • S Sevententh

          Thanks... this is very close to what I needed :-D

          S Offline
          S Offline
          Sevententh
          wrote on last edited by
          #4

          Worked it out... for those that would like to know

          public partial class App : Application
          {
          private static ComboLookupViewModel comboLookups = new ComboLookupViewModel();

              public static ComboLookupViewModel ComboBoxLookups
              {
                  get { return comboLookups; }
               }
          }
          
                      <ListView x:Name="lstTimeZones" 
                                **ItemsSource="{Binding Path=ComboBoxLookups.CurrentTimeZone, Source={x:Static Application.Current}}"**
                                ScrollViewer.CanContentScroll="True"
                                SelectionMode="Single" 
                                SelectionChanged="lstTimeZones\_SelectionChanged"
                                Background="Transparent" BorderBrush="Transparent">
                          <ListView.View>
                              <GridView>
                                  <GridViewColumn Header="UTC Name" Width="120" DisplayMemberBinding="{Binding Path=UTCName}"/>
                                  <GridViewColumn Header="UTC Value" Width="120" DisplayMemberBinding="{Binding Path=UTCValue}"/>
                              </GridView>
                          </ListView.View>
                      </ListView>
          

          I now have ObservableCollections<> in my class that are updated via CRUD and changes shown when reference on another UserControl :cool:

          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