2016-09-29 112 views
0

我正在寫一個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

+0

如果您嘗試使用INotifyPropertyChanged,則必須在有界屬性的setter中調用NotifyPropertyChanged:public int Minutes {set {NotifyPropertyChanged();}}並且必須在SelectGoalTime類中設置DataContext。 –

+0

糟糕,忘記刪除該實現。它不應該影響這個問題,因爲在頁面加載和選擇之外這個值沒有改變(即這個屬性不需要INotifyPropertyChanged實現)。爲什麼我的列表綁定可以在沒有設置DataContext的情況下工作? – Ceshion

回答

0

由於我沒有信譽添加評論,我不得不分享這樣:

這裏找到,https://twitter.com/kdawg02/status/746734845393518592,BUG UWP組合框的SelectedValue必須在XAML的最後一個屬性,否則不會在加載時設置該值。

希望它有幫助,我試圖在UWP中綁定一個ComboBox與MVVM模式並沒有麻煩。

+0

不幸的是,這似乎沒有解決它。它似乎正在加載視圖模型中的值,但它沒有設置它。 – Ceshion