代码之家  ›  专栏  ›  技术社区  ›  k4yaman

wpf mvvm绑定相对源

  •  2
  • k4yaman  · 技术社区  · 6 年前

    我在和相关的消息来源做斗争。我在tab项中,我想访问父modelview。 目标是使某些上下文菜单项不可见(如果该项是最后一个选项卡)。

    ViewModel:

    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>
    

    当我把它放在item类中时,它就工作了,但是那里没有关于还剩多少个标签的信息,在这里实现一个messenger似乎是反逻辑的。

    1 回复  |  直到 6 年前
        1
  •  2
  •   FrankM    6 年前

    你已经定义了 IsLastTab 这样地:

    public bool IsLastTab => ItemCollection.Count > 1;
    

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

    你需要这样的东西:

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

    (因为您没有发布底层视图模型,所以这只是一个猜测。准确的代码可能会改变。)

    你还需要打电话 INotifyPropertyChanged.PropertyChanged 对于每个选项卡项的属性“islasttab”,只要选项卡集合发生更改。