更新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();
}
}
事件ItemsSource for combobox不起作用?你可以在這裏發佈所有的MainPage.xaml嗎? – Nagg 2013-03-06 11:05:19
BTW:new Action(async()=>等待新的Windows.UI.Popups.MessageDialog(「Testing dialog」)。ShowAsync())。Invoke(); ---它炸了我的腦海 – Nagg 2013-03-06 11:06:03
組合框沒有得到ItemsSource和按鈕單擊不起作用。請參閱示例[這裏](https://skydrive.live.com/redir?resid=9E70869A1583FA52!892&authkey=!AHEXR2PtckK9gJM) – Xyroid 2013-03-06 11:17:24