将菜单项绑定到命令

时间:2014-06-24 15:24:21

标签: c# wpf

我正在尝试将命令绑定到视图模型,但它没有触发

    xmlns:ViewModel="clr-namespace:Lister.WPF.ViewModels"
    DataContext="{Binding ViewModel:TweetViewModel}" Icon="pack://siteoforigin:,,,/Resources/internet_down_16_hot.png" Loaded="Window_Loaded">
<Grid>
    <!--Test-->
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem BorderThickness="5" Header="Status">
                <MenuItem BorderThickness="5" Header="Timeline" Command="{Binding  RefreshTweetsCommand}"/>

以下是VM的代码

    private RelayCommand _refreshTweetsCommand;
    public RelayCommand RefreshTweetsCommand
    {
        get
        {
            return _refreshTweetsCommand ??
                   (_refreshTweetsCommand =
                    new RelayCommand(RefreshTweetList, LoadTweets));
        }
    }

1 个答案:

答案 0 :(得分:2)

DataContext="{Binding ViewModel:TweetViewModel}"这无法绑定DataContext

按如下方式设置DataContext:

<Window>
  <Window.DataContext>
    <ViewModel:TweetViewModel/>
  </Window.DataContext>
</Window>