我正在写一个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
如果您尝试使用INotifyPropertyChanged,则必须在有界属性的setter中调用NotifyPropertyChanged:public int Minutes {set {NotifyPropertyChanged();}}并且必须在SelectGoalTime类中设置DataContext。 –
糟糕,忘记删除该实现。它不应该影响这个问题,因为在页面加载和选择之外这个值没有改变(即这个属性不需要INotifyPropertyChanged实现)。为什么我的列表绑定可以在没有设置DataContext的情况下工作? – Ceshion