是否有可能在ModelView中调用方法时,ribbontab选择的变化

本文关键字:ribbontab 选择 变化 方法 有可能 ModelView 调用 是否 | 更新日期: 2023-09-27 18:02:07

我正在寻找一种简单的方法来显示用户控制时,不同的ribbontab被选中

这是我的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;
    }
}

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

是否有可能在ModelView中调用方法时,ribbontab选择的变化

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

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