当ribbontab选择更改时,是否可以在ModelView中调用方法?

时间:2014-05-07 15:50:29

标签: c# wpf mvvm ribbon

我正在搜索选择不同功能区选项卡时显示usercontrol的简单方法。

这是我的xaml:

<ribbon:RibbonWindow x:Class="msr.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        xmlns:msrlib="clr-namespace:msrlib;assembly=msrlib"
        xmlns:msrui="clr-namespace:msr.View.Controls"
        msrlib:DialogService.IsRegisteredView="True"
        Title="MainWindow" 
        Height="700" 
        Width="1260"
        MaxHeight="700"
        MaxWidth="1260">

    <DockPanel>
        <ribbon:Ribbon DockPanel.Dock="Top" Title="App">
            <ribbon:RibbonTab Header="Tab1" IsSelected="True">
                <ribbon:RibbonGroup>
                    <ribbon:RibbonToggleButton IsChecked="True" Command="{Binding ChangeAnalisysView}" CommandParameter="Analisys" SmallImageSource="Images\en\ribbon_button_jan16.png" LargeImageSource="Images\en\ribbon_button_jan32.png" Label="Label1"/>
                    <ribbon:RibbonToggleButton Command="{Binding ChangeAnalisysView}" CommandParameter="AnalisysZestawienieUmow" SmallImageSource="Images\en\ribbon_button_jan16.png" LargeImageSource="Images\en\ribbon_button_jan32.png" Label="Label2"/>
                </ribbon:RibbonGroup>
            </ribbon:RibbonTab>
            <ribbon:RibbonTab Header="Użytkownicy" x:Name="Users">
                <ribbon:RibbonGroup>
                    <ribbon:RibbonButton Command="{Binding UsersViewModel.AddUserCommand}" SmallImageSource="Images\en\ribbon_button_jan16.png" LargeImageSource="Images\en\ribbon_button_jan32.png" Label="Nowy użytkownik"/>
                    <ribbon:RibbonButton Command="{Binding UsersViewModel.EditUserCommand}" SmallImageSource="Images\en\ribbon_button_jan16.png" LargeImageSource="Images\en\ribbon_button_jan32.png" Label="Edytuj"/>
                    <ribbon:RibbonButton Command="{Binding UsersViewModel.DeleteUserCommand}" SmallImageSource="Images\en\ribbon_button_jan16.png" LargeImageSource="Images\en\ribbon_button_jan32.png" Label="Usuń"/>
                </ribbon:RibbonGroup>
            </ribbon:RibbonTab>
        </ribbon:Ribbon>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <ContentControl Visibility="{Binding CurrentView, Converter={StaticResource StringToVisibilityConverter}, ConverterParameter=Analisys}">
            <msrui:Analysis Grid.Row="0" DataContext="{Binding AnalisysViewModel}"/>
            </ContentControl>

            <ContentControl Visibility="{Binding CurrentView, Converter={StaticResource StringToVisibilityConverter}, ConverterParameter=Users}">
                <msrui:Users Grid.Row="0" DataContext="{Binding UsersViewModel}"/>
            </ContentControl>

            <ContentControl Visibility="{Binding CurrentView, Converter={StaticResource StringToVisibilityConverter}, ConverterParameter=AnalisysZestawienieUmow}">
                <msrui:AnalisysZestawienieUmow Grid.Row="0" DataContext="{Binding AnalisysZestawienieUmowViewModel}"/>
            </ContentControl>
        </Grid>
    </DockPanel>
</ribbon:RibbonWindow>

我的MainWindowViewModel:

class MainWindowViewModel : ViewModelBase
{
    //TODO ICommand for navigation
    public MainWindowViewModel()
    {
        HideWorkspace = true;
        AnalisysViewModel = new Analisys();
        UsersViewModel = new UsersViewModel();
        AnalisysZestawienieUmowViewModel = new AnalisysZestawienieUmow();
        CurrentView = "Analisys";
    }


    public Analisys AnalisysViewModel { get; private set; }
    public UsersViewModel UsersViewModel { get; private set; }
    public AnalisysZestawienieUmow AnalisysZestawienieUmowViewModel { get; private set; }
    public string CurrentView { get; set; }
}

我计划使用“CurrentView”来设置应该显示的用户控件的可见性。为此,我使用自己的StringToVisibilityConverter:

public class StringToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = value as string;

        if (string.IsNullOrEmpty(s)) {
            return Visibility.Collapsed;
        };

        //value is not empty
        string param = (string)parameter;
        //no param, show
        if (string.IsNullOrEmpty(param))
        {
            return Visibility.Visible;
        }

        //got param
        if (s == param) return Visibility.Visible;

        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

功能区:RibbonToggleButton具有Command属性,因此我可以在主视图模型中创建ICommand并设置CurrentView属性。我不知道如何为RibbonTab创建类似的命令。

1 个答案:

答案 0 :(得分:0)

将RibbonTab的IsSelected属性绑定到ViewModel中的属性:

public bool Tab1Selected { 
    get { return tab1Selected; } 
    set { tab1Selected = value; 
          if (tab1Selected) {    
              CallMyMthod(); 
        } 
    } 
}