How do you access / databind a Global ObservableCollection<> in XAML
-
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
-
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
-
Have a look at this Binding to global App properties article[^] by Sacha Barber.
Thanks... this is very close to what I needed :-D
-
Thanks... this is very close to what I needed :-D
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: