MVVM和功能区命令

时间:2010-04-09 01:28:50

标签: c#

我正在尝试在SampleCommand超时时更改Page1.xaml中帧的source属性。 我如何在View Model中实现这一点?

的Page1.xaml:


            <r:RibbonTab.Groups>
                <r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayout}">
                    <r:RibbonGroup.Command>
                        <r:RibbonCommand LabelTitle="RibbonButton"/>
                    </r:RibbonGroup.Command>
                    <r:RibbonButton x:Name="RibbonButton1" Command="{Binding Path=SampleCommand}"/>
                </r:RibbonGroup>
            </r:RibbonTab.Groups>
        </r:RibbonTab>


    </r:Ribbon>

    <Border Name="PageBorder" Grid.Row="0" Grid.Column="1">
        <Frame Name="pageFrame" Source="FirstPage.xaml" />

    </Border>
</DockPanel>

C# Page1ViewModel.cs:

RelayCommand _sampleCommand;

    public ICommand SampleCommand
    {
        get
        {
            // create command ??

            return _sampleCommand 
    }

page1.xaml.cs:

Page1ViewModel pageViewModel;

this.DataContext = pageViewModel; //当pageloads

1 个答案:

答案 0 :(得分:2)

你试过这种方式吗?

public class ViewModel{

private SimpleCommand divertCommand;


public ViewModel()
{
testCommand = new SimpleCommand
        {
            CanExecuteDelegate = x => true,
            ExecuteDelegate = x => ExecuteCommand()
        };
}

        public SimpleCommand DivertCommand
        {
            get { return divertCommand; }
        }

        private void ExecuteCommand()
        {
            DivertCommand.CommandSucceeded = false;

//Your code to execute

            DivertCommand.CommandSucceeded = true;
        }}
}

请使用此项目作为参考:link

有一个很好的帖子here

祝你好运

里克