简体   繁体   English

WPF / MVVM-标题绑定

[英]WPF / MVVM - Title Binding

I'm using MVVM pattern in WPF. 我在WPF中使用MVVM模式。 My small project contains 1x Window and 3x UserControls. 我的小项目包含1个Window和3个UserControl。 Each UserControl represents tab, please see the picture below. 每个UserControl代表一个标签,请参见下图。

Solution structure 解决方案结构

What I'm trying to do is to bind the Window Title to the variable in each tab model. 我想做的是将窗口标题绑定到每个选项卡模型中的变量。 I tried to use Interaction.Triggers and event name "Loaded". 我尝试使用Interaction.Triggers和事件名称“ Loaded”。 The command "ChangeTitle" works fine when I switch between different views but when I select tab with the same view as previously selected tab then title doesn't change until I switch to another tab with different view. 当我在不同的视图之间切换时,命令“ ChangeTitle”可以很好地工作,但是当我选择与先前选择的选项卡具有相同视图的选项卡时,标题不会改变,直到我切换到另一个具有不同视图的选项卡。 I tried other events but couldn't find any for this purpose. 我尝试了其他活动,但找不到任何目的。 I would like to trigger the above command after tab selection changed. 选项卡选择更改后,我想触发上述命令。 Please advise. 请指教。 Thank you. 谢谢。

MainWindow.xaml MainWindow.xaml

<Window x:Class="LSS_doc.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:models="clr-namespace:LSS_doc.Models"
    xmlns:Views="clr-namespace:LSS_doc.Views"
    mc:Ignorable="d"
    Height="350" Width="525"
    MinHeight="350"
    MinWidth="525">

<Grid>
    <intr:Interaction.Triggers>
        <intr:EventTrigger EventName="Loaded">
            <intr:InvokeCommandAction Command="{Binding CreateMainTab}"/>
        </intr:EventTrigger>
    </intr:Interaction.Triggers>
    <DockPanel>
        <TabControl Name="tabControl" ItemsSource="{Binding Tabs}" SelectedIndex="{Binding TabIndex}">
            <TabControl.Resources>
                <DataTemplate DataType="{x:Type models:MainTabModel}">
                    <Views:MainTabView/>
                </DataTemplate>
                <DataTemplate DataType="{x:Type models:ResultTabModel}">
                    <Views:ResultTabView/>
                </DataTemplate>
                <DataTemplate DataType="{x:Type models:DisplayTabView}">
                    <Views:DisplayTabView/>
                </DataTemplate>
            </TabControl.Resources>

            <TabControl.ItemTemplate>
                <DataTemplate DataType="{x:Type models:ITabModel}">
                    <TextBlock>
                    <Run Text="{Binding Name}"/>
                    <Hyperlink Command="{Binding CloseCommand}" ><Run Text="{Binding CloseButton}"/></Hyperlink>
                    </TextBlock>
                </DataTemplate>
            </TabControl.ItemTemplate>
        </TabControl>
    </DockPanel>
</Grid>

MainTabView.xaml MainTabView.xaml

<UserControl x:Class="LSS_doc.Views.MainTabView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         mc:Ignorable="d"

         d:DesignHeight="450" d:DesignWidth="800">
<intr:Interaction.Triggers>
    <intr:EventTrigger EventName="Loaded">
        <intr:InvokeCommandAction CommandParameter="{Binding Name}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ChangeTitle}"/>
    </intr:EventTrigger>
</intr:Interaction.Triggers>
<Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="100"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox Name="searchBox" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Keywords}" Margin="10,10,10,10" Height="30"/>
    <Button Name="searchButton" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Search}" Grid.Column="1" Margin="0,0,10,0" Height="30"/>
    <TreeView Grid.Row="1" Grid.ColumnSpan="2" Margin="10,0,10,10" BorderThickness="0">
        <TreeViewItem Header="{Binding Name}" IsExpanded="True">
            <TreeViewItem Header="Level 2.1" />
            <TreeViewItem Header="Level 2.2" IsExpanded="True">
                <TreeViewItem Header="Level 3.1" />
                <TreeViewItem Header="Level 3.2" />
            </TreeViewItem>
            <TreeViewItem Header="Level 2.3" />
        </TreeViewItem>
    </TreeView>
</Grid>

ResultTabView.xaml ResultTabView.xaml

<UserControl x:Class="LSS_doc.Views.ResultTabView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<intr:Interaction.Triggers>
    <intr:EventTrigger EventName="Loaded">
        <intr:InvokeCommandAction CommandParameter="{Binding Name}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ChangeTitle}"/>
    </intr:EventTrigger>
</intr:Interaction.Triggers>
<Grid>
    <ListBox x:Name="FileList" ItemsSource="{Binding Result}">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="string">
                <TextBlock>
                    <Hyperlink CommandParameter="{Binding}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.DisplayFile}">
                        <TextBlock Text="{Binding}"/>
                    </Hyperlink>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

DisplayTabView.xaml DisplayTabView.xaml

<UserControl x:Class="LSS_doc.Views.DisplayTabView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:LSS_doc.Helpers"
         xmlns:ns="clr-namespace:LSS_doc.Helpers"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<intr:Interaction.Triggers>
    <intr:EventTrigger EventName="Loaded">
        <intr:InvokeCommandAction CommandParameter="{Binding Name}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ChangeTitle}"/>
    </intr:EventTrigger>
</intr:Interaction.Triggers>
<Grid >
    <WebBrowser local:WebBrowserExtensions.BindableSource="{Binding FileUrl}" local:WebBrowserExtensions.BindableLoaded="{Binding AcceptedKeywords}"/>
</Grid>

您可以使用ElementName将窗口标题直接绑定到模型:

<Window Title="{Binding ElementName=tab,Path=SelectedItem.WindowTitleForThisTab}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM