UWP ComboBox没有填充绑定值

时间:2016-09-29 21:07:56

标签: c# xaml combobox uwp

我正在编写一个UWP应用程序,并且有几个组合框绑定到我的视图模型。由于某些原因,组合框不会更新绑定值,也不会在渲染时加载它,如果我在调试时手动设置值。我看到这是一个常见的问题,但我无法发现我见过其他人到目前为止的任何原因。以下是我的精简代码:

XAML:

<Page
x:Class="UWPApp.Scorekeeper.SelectGoalTime"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWPApp.Scorekeeper"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="MainElement">

<Grid Background="{ThemeResource SystemControlBackgroundAccentBrush}">
    <ComboBox x:Name="MinutesSelect" SelectedValue="{Binding ElementName=MainElement,Path=ViewModel.Minutes}" ItemsSource="{Binding ElementName=MainElement,Path=MinutesList}"/>
</Grid>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using UWPApp.Scorekeeper.Interfaces;
using UWPApp.Scorekeeper.Models.ViewModels;
using UWPApp.Scorekeeper.Models.TransportClasses;
using Windows.UI.Popups;
using UWPApp.Scorekeeper.Models;
using UWPApp.Scorekeeper.Toolbox;

namespace UWPApp.Scorekeeper
{
    public sealed partial class SelectGoalTime : Page
    {
        public AddGoal_FVM ViewModel { get; set; }

        public List<int> MinutesList { get; set; } = Enumerable.Range(0,21).ToList();

        public SelectGoalTime()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var message = e.Parameter as GoalMessage;
            ViewModel = message.ViewModel;
        }
    }
}

AddGoal_FVM

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace UWPApp.Scorekeeper.Models.ViewModels
{
    public class AddGoal_FVM
    {
        public int Minutes { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

由于我不具备添加评论的声誉,我必须以这种方式分享:

在此处找到https://twitter.com/kdawg02/status/746734845393518592,BUG UWP ComboBox SelectedValue必须是XAML中的最后一个属性,否则它将不会在加载时设置该值。

希望它有所帮助,尝试将UWP中的组合框与MVVM模式绑定,我没有遇到麻烦。