c# - 使用 MVVm 浏览 TabItem

标签 c# wpf xaml mvvm

假设我有 3 个用户控件(TIShowNames、TIEnterCode、TIShowFactor)。 他们有自己的观点和相应的 viewModel。

所有这 3 个,都在 mainwindowView 中。

这是我的主窗口 View Xaml:

<Controls:TransitionPresenter Name="transContainer" Grid.Row="2" RestDuration="0:0:1" IsLooped="False" Transition="{StaticResource SlideTransition}">
        <TabControl Name="TCMain" Background="#00FFFFFF" BorderThickness="0" Padding="0 -5 0 0 ">



            <TabItem Name="TIShowNames" Visibility="Collapsed">
                <views:NameView x:Name="NameViewElement" />
            </TabItem>
            <TabItem Name="TIEnterCode" Visibility="Collapsed">
                 <views:CodeView x:Name="CodeViewElement"  />
            </TabItem>
            <TabItem Name="TIShowFactor" Visibility="Collapsed">
                <views:FactorDetailView x:Name="FactorDetailViewElement"  />
            </TabItem> 

        </TabControl>
    </Controls:TransitionPresenter>

在我以前的编程风格中,我曾经使用这行代码来浏览选项卡项(没有任何模式):

 private void ChangeTabItemTo(TabItem TI)
    {

        transContainer.ApplyTransition("TCMain", "TCMain");
        TCMain.SelectedItem = TI;
    }

我在“TIShowNames”中有一个 btn 节目,所以当我点击它时,它必须转到“TIShowFactor”。 在 MVVM 中,ViewModel 对 View 一无所知(此项选项卡在其父 View 中!!!)。那么他如何在不违反 MVVM 的情况下更改选定的选项卡项??

另一个尝试: 由于此错误,无法更改 Selectedindex:

"System.Windows.Data Error: 40 : BindingExpression path error: 'Index' property not found on 'object' ''MainWindowViewModel' (HashCode=22018304)'. BindingExpression:Path=AAA; DataItem='MainWindowViewModel' (HashCode=22018304); target element is 'TabControl' (Name=''); target property is 'IsSelected' (type 'Boolean')"

更新:

Controls:TransitionPresenter 来自 Fluid DLL

更新:

我想隐藏选项卡项的标题,这样没有人可以单击标题,并且只能通过用户控件中的 btns 才能通过标题导航

最佳答案

您可以为 View 中的每个 View 模型类型定义一个DataTemplate:

<TabControl Name="TCMain"
            ItemsSource="{Binding ViewModels}"
            SelectedItem="{Binding ViewModel}"
            Background="#00FFFFFF" BorderThickness="0" Padding="0 -5 0 0 ">
    <TabControl.ContentTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding}">
                <ContentControl.Resources>
                    <DataTemplate DataType="{x:Type local:NameViewViewModel}">
                        <views:NameView />
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type local:CodeViewViewModel}">
                        <views:CodeView />
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type local:FactorDetailViewModel}">
                        <views:FactorDetailView />
                    </DataTemplate>
                </ContentControl.Resources>
            </ContentControl>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

...并将 SelectedItem 属性绑定(bind)到您在 View 模型中设置的源属性,例如:

public object ViewModel
{
    get { return _vm; }
    set { _vm = value; NotifyPropertyChanged(); }
}
...
ViewModel = new CodeViewViewModel(); //displays the CodeView

关于c# - 使用 MVVm 浏览 TabItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46649164/

相关文章:

c# - 从自定义 header 和请求正文绑定(bind)

c# - 通过 C# 代码打开 excel 时不执行宏,而手动打开 excel 时执行 VBA 代码

c# - 在上下文菜单中设置文本框的焦点 - wpf

c# - Azure session 缓存错误

java - 策略模式的真实示例

WPF 相对源行为

wpf - 如何在 Windows Presentation Foundation (WPF) 中实现本地化?

wpf - 如何自定义通用异常消息 "Value ' ' 无法转换“

wpf - 从 DataTemplateSelector 显式刷新 DataTemplate?

c# - 我可以将 DataContext 设置为静态类吗?