none
Binding TimeZoneInfo to Combobox RRS feed

  • Question

  • Using VS2107 Community

    I'm trying to bind a combobox to a readonly collection of timezoninfo without success.

    I'm using VB for code.

    My XAML - to show the page setup and just the combobox looks like:

    <Page
        x:Class="SunObservation.JobPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:SunObservation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Loaded="Page_Loaded"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
        <Page.Resources>
            <Style TargetType="RadioButton" x:Key="RBTime">
                <Setter Property="Margin" Value="20,10,10,10"/>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="GroupName" Value="Time"/>
            </Style>
        </Page.Resources>
        
        <Page.BottomAppBar>
            <CommandBar>
                <AppBarButton x:Name="AddJobBtn" Label="New Job" Icon="Add" Click="BtnNewJob_Click" />
                <AppBarButton x:Name="SaveJobBtn" Label="Save Job" Icon="Save" Click="BtnSaveJob_Click" />
            </CommandBar>
        </Page.BottomAppBar>
        
        <Grid>
    <ComboBox Name="CboTimeZone"
                          DisplayMemberPath="DisplayName"
                          Header="Time Zone"
                          ItemsSource="{Binding timezones}"
                          SelectedItem="{Binding Id}"
                          Width="300"
                          Margin="20,0,5,0"
                          FontSize="16"
                          RelativePanel.AlignLeftWithPanel="True"/>
    </Grid>
    
    </Page>

    and the code behind;

    Public NotInheritable Class JobPage
        Inherits Page
        Private bIsInitialising As Boolean = True
        Public timeZones As ReadOnlyCollection(Of TimeZoneInfo) = TimeZoneInfo.GetSystemTimeZones()
    
        Private Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
    
            Try
                CboTimeZone.SelectedItem = "Utc"
                SetJobBindings()
            Catch ex As Exception
                Dim dialog = New MessageDialog("Error : " & ex.Message) With {
                    .Title = "Page_Loaded"
                }
                Dim res = dialog.ShowAsync()
            End Try
    
        End Sub
    
    ...
    

    The app builds and displays but with an empty combobox.

    I'm very very new to UWP.  All help appreciated.

    Rinso



    • Edited by Rinso Monday, July 22, 2019 8:02 AM error
    Monday, July 22, 2019 7:58 AM

Answers

  • Hi Rinso,

    I checked your code, there're three issues in it.

    1. the binding is bound to a property, not a variable
    2. You bind ItemsSource to 'timezones', but your define a 'timeZones' in code-behind. 'Z' is capitalized.
    3. If you use binding, you need to specify DataContext.

    So, the code should look like the following:

    Public NotInheritable Class MainPage
        Inherits Page
        Public Property timeZones As ReadOnlyCollection(Of TimeZoneInfo)
        Public Sub New()
            InitializeComponent()
            timeZones = TimeZoneInfo.GetSystemTimeZones()
            Me.DataContext = Me
        End Sub
    End Class
    <ComboBox Name="CboTimeZone"
                          ItemsSource="{Binding timeZones, Mode=OneWay}"
                          DisplayMemberPath="DisplayName"
                          Header="Time Zone"
                          SelectedValue="{Binding Id, Mode=OneWay}"
                          Width="300"/>


    Best Regards,

    Xavier


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rinso Thursday, July 25, 2019 1:52 AM
    Wednesday, July 24, 2019 8:20 AM
    Moderator

All replies

  • Hi Rinso,

    Your code sample was imcomplete. I cannot use it to reproduce your issue. For example, what's the 'SetJobBinding()' method?

    Please provide a minimal reproducible code sample.

    Best Regards,

    Xavier


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, July 22, 2019 11:08 AM
    Moderator
  • Hi Xavier, that procedure was a code behind to set the bindings for other objects on the page.  That was working fine and I have commented it out now.  In response to your request, here is the minimal reproducible code sample.

    For XAML;

    <Page
        x:Class="SunObservation.BlankPage1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:SunObservation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
        <Grid>
            <ComboBox Name="CboTimeZone"
                          ItemsSource="{Binding timezones, Mode=OneWay}"
                          DisplayMemberPath="DisplayName"
                          Header="Time Zone"
                          SelectedValue="{Binding Id, Mode=OneWay}"
                          Width="300"/>
        </Grid>
    </Page>

    and for the code behind;

    ' The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
    
    ''' <summary>
    ''' An empty page that can be used on its own or navigated to within a Frame.
    ''' </summary>
    Public NotInheritable Class BlankPage1
        Inherits Page
        Public timeZones As ReadOnlyCollection(Of TimeZoneInfo) = TimeZoneInfo.GetSystemTimeZones()
    End Class

    The project builds and runs with an empty combobox.

    Thank you.

    Rinso.

     
    Wednesday, July 24, 2019 6:38 AM
  • Hi Rinso,

    I checked your code, there're three issues in it.

    1. the binding is bound to a property, not a variable
    2. You bind ItemsSource to 'timezones', but your define a 'timeZones' in code-behind. 'Z' is capitalized.
    3. If you use binding, you need to specify DataContext.

    So, the code should look like the following:

    Public NotInheritable Class MainPage
        Inherits Page
        Public Property timeZones As ReadOnlyCollection(Of TimeZoneInfo)
        Public Sub New()
            InitializeComponent()
            timeZones = TimeZoneInfo.GetSystemTimeZones()
            Me.DataContext = Me
        End Sub
    End Class
    <ComboBox Name="CboTimeZone"
                          ItemsSource="{Binding timeZones, Mode=OneWay}"
                          DisplayMemberPath="DisplayName"
                          Header="Time Zone"
                          SelectedValue="{Binding Id, Mode=OneWay}"
                          Width="300"/>


    Best Regards,

    Xavier


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rinso Thursday, July 25, 2019 1:52 AM
    Wednesday, July 24, 2019 8:20 AM
    Moderator
  • Thanks Xavier.

    I was vaguely aware of the requirement to bind to a property but not sure how to in this case.

    I note that setting the DataContext uses the Me operator.  I have modified my code as follows;

    CboTimeZone.DataContext = Me

    as I wish to add other controls to the page bound to another Class defined in my project.  Setting the DataContext that way still displays the required timeZones results.

    I will do some testing and research before my next question.

    Cheers,

    Rinso

    Thursday, July 25, 2019 3:02 AM