2013-03-06 57 views
1

更新1:您可以從here下載示例項目。無法在WinRT MVVM中更改按鈕點擊和組合框選擇

你能幫我找到我的代碼中的錯誤。我無法將項目源分配給組合框以及WinRT應用程序中的按鈕單擊事件。我正在使用MVVM和MetroEventToCommand。我對MVVM概念很陌生,所以請回答我這個愚蠢的問題。

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
    <Button Content="Click Here"> 
     <mvvm:EventToCommandManager.Collection> 
      <mvvm:EventToCommand Command="{Binding ButtonClickCommand}" Event="Click"/> 
     </mvvm:EventToCommandManager.Collection> 
    </Button> 

    <ComboBox x:Name="FontsCombo" Height="50" Width="150" SelectedItem="{Binding SelectedFont}" ItemsSource="{Binding fonts}" /> 

    <TextBlock FontSize="30" Text="{Binding SelectedFont}"/> 
</Grid> 

public MainPage() 
{ 
    this.InitializeComponent(); 
    this.DataContext = new VM(); 
} 

public class VM : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public RelayCommand ButtonClickCommand { get; set; } 

    private ObservableCollection<string> _fonts = new ObservableCollection<string>(); 
    public ObservableCollection<string> fonts 
    { 
     get { return _fonts; } 
     set 
     { 
      _fonts = value; 
      if (null != PropertyChanged) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("fonts")); 
      } 
     } 
    } 

    private string _SelectedFont = ""; 
    public string SelectedFont 
    { 
     get { return _SelectedFont; } 
     set 
     { 
      // Some logic here 
      _SelectedFont = value; 
      if (null != PropertyChanged) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("SelectedFont")); 
      } 

     } 
    } 
    public VM() 
    { 
     fonts.Add("Arial"); 
     fonts.Add("Courier New"); 
     fonts.Add("Times New Roman"); 
     ButtonClickCommand = new RelayCommand(Click); 
    } 

    private void Click() 
    { 
     new Action(async() => await new Windows.UI.Popups.MessageDialog("Testing dialog").ShowAsync()).Invoke(); 
    } 
} 
+0

事件ItemsSource for combobox不起作用?你可以在這裏發佈所有的MainPage.xaml嗎? – Nagg 2013-03-06 11:05:19

+0

BTW:new Action(async()=>等待新的Windows.UI.Popups.MessageDialog(「Testing dialog」)。ShowAsync())。Invoke(); ---它炸了我的腦海 – Nagg 2013-03-06 11:06:03

+0

組合框沒有得到ItemsSource和按鈕單擊不起作用。請參閱示例[這裏](https://skydrive.live.com/redir?resid=9E70869A1583FA52!892&authkey=!AHEXR2PtckK9gJM) – Xyroid 2013-03-06 11:17:24

回答

1

對於的SelectedItem,你沒有指定模式=雙向:

<ComboBox x:Name="FontsCombo" Height="50" Width="150" SelectedItem="{Binding SelectedFont, Mode=TwoWay}" ItemsSource="{Binding fonts}" /> 

編輯 我找到了解決辦法:

public class VM : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public RelayCommand ButtonClickCommand { get; set; } 

    private ObservableCollection<string> _fonts; 
    public ObservableCollection<string> fonts 
    { 
     get { return _fonts; } 
     set 
     { 
      _fonts = value; 
      if (null != PropertyChanged) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("fonts")); 
      } 
     } 
    } 

    private string _SelectedFont; 
    public string SelectedFont 
    { 
     get { return _SelectedFont; } 
     set 
     { 
      // Some logic here 
      _SelectedFont = value; 
      if (null != PropertyChanged) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("SelectedFont")); 
      } 

     } 
    } 
    public VM() 
    { 
     this.fonts = new ObservableCollection<string>(); 
     fonts.Add("Arial"); 
     fonts.Add("Courier New"); 
     fonts.Add("Times New Roman"); 
     ButtonClickCommand = new RelayCommand(Click); 
    } 

    private void Click() 
    { 
     new Action(async() => await new Windows.UI.Popups.MessageDialog("Testing dialog").ShowAsync()).Invoke(); 
    } 
} 

如果在構造我的實例字體, UX不再凍結。

+0

不工作,我已經上傳了我的[項目](https://skydrive.live.com/redir?resid = 9E70869A1583FA52!892&authkey =!AHEXR2PtckK9gJM)請看看 – Xyroid 2013-03-06 11:15:28

+0

當我嘗試你的項目時,UX被凍結。它也在你的項目上嗎? – 2013-03-06 11:26:03

+0

是的,我不能點擊按鈕,也無法打開組合框 – Xyroid 2013-03-06 11:39:29