未使用绑定值填充UWP组合框

本文关键字:组合 UWP 填充 绑定 未使用 | 更新日期: 2023-09-27 18:09:46

我正在编写一个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; }
    }
}

未使用绑定值填充UWP组合框

由于我没有添加评论的声誉,我不得不这样分享:

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

希望它能有所帮助,我在尝试用MVVM模式绑定UWP中的组合框时遇到了很多麻烦。