c# - WPF MVVM 绑定(bind)相关源

标签 c# wpf mvvm

我正在努力处理相关来源。我位于选项卡项内,并且想要访问父模型 View 。 目标是使某些上下文菜单项不可见(如果该项目是最后一个选项卡)。

View 模型:

public bool IsLastTab => ItemCollection.Count > 1;

xaml:

<Window x:Name="MainWinodw"
    ...
    xmlns:ct="clr-namespace:ChromeTabs;assembly=ChromeTabs"
    xmlns:vm="clr-namespace:Main.ViewModel"
    xmlns:conv="clr-namespace:Main.Converters"
    xmlns:ctConv="clr-namespace:ChromeTabs.Converters;assembly=ChromeTabs"
    xmlns:usercontrols="clr-namespace:Main.UserControls"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    ...
    DataContext="{Binding Source={StaticResource Locator},Path=Main}" ">
<Window.Resources>
    <conv:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />
    ...
</Window.Resources >
<Grid>
    <ct:ChromeTabControl x:Name="MyChromeTabControl"
                         TabPersistBehavior="Timed"
                         TabPersistDuration="0:0:0:5"
                         AddTabButtonBehavior="OpenNewTab"
                         Background="AliceBlue"
                         ItemsSource="{Binding ItemCollection}"
                          ...">
        ...
        <ct:ChromeTabControl.ItemTemplate>
            <DataTemplate>
                <Grid Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type ct:ChromeTabItem}}}">
                    ...
                    <Grid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Close"
                                      Command="{Binding Path=PlacementTarget.Tag.CloseTabCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                                      Visibility="{Binding IsLastTab,  
                                                    RelativeSource={RelativeSource AncestorType={x:Type vm:ViewModelChromeTabs}}, 
                                                    Mode=OneWay, 
                                                    Converter={StaticResource InverseBooleanToVisibilityConverter}}"
                                      CommandTarget="{Binding Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
                            ...
                            <Separator />
                            <MenuItem Header="{Binding IsPinned, Converter={StaticResource BooleanToPinTabTextConverter}}"
                                      Command="{Binding Path=PlacementTarget.Tag.PinTabCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                                      CommandTarget="{Binding Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                                      CommandParameter="{Binding}" />
                        </ContextMenu>
                    </Grid.ContextMenu>
                </Grid>
            </DataTemplate>
        </ct:ChromeTabControl.ItemTemplate>
    </ct:ChromeTabControl>
</Grid>
</Window>

当我将它放入项目类中时它正在工作,但我没有关于剩余多少个选项卡的信息,并且为此实现一个信使似乎是反逻辑的。

最佳答案

您已经像这样定义了 IsLastTab:

public bool IsLastTab => ItemCollection.Count > 1;

这似乎无法确定当前选项卡是否是最后一个选项卡。

你需要这样的东西:

public bool IsLastTab => ReferenceEquals(this, ItemCollection.LastOrDefault());

(由于您没有发布底层 View 模型,这只是一个猜测。确切的代码可能会改变。)

您还需要调用INotifyPropertyChanged.PropertyChanged每当选项卡集合更改时,每个选项卡项的属性“IsLastTab”。

关于c# - WPF MVVM 绑定(bind)相关源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907864/

相关文章:

c# - lock(syncRoot) 对静态方法有何意义?

c# - 删除 XmlDocument 中的自关闭标签(例如/>)

c# - 失去焦点后在 ElementHost 的 WPF RichTextBox 中显示选择

wpf - 将 View 拆分为两个单独的 View

wpf - 如何在一个集合上定义 2 个单独的过滤器?

C# System.Threading.Timer 递归更新

c# - 在 C# 中的单个 Oracle 命令中执行多个查询

.net - 是什么导致 WPF ListCollectionView 使用自定义排序来重新排序其项目?

c# - 放大或缩小时如何使 Canvas 区域在 ScrollViewer 中居中,而不是所有内容都可以显示在查看窗口中

c# - Windows 8 应用商店应用全局 View 模型