WPF:隐藏绑定到可观察集合的选项卡控件中的选项卡项

blu*_*bit 3 wpf tabcontrol mvvm hide tabitem

我有一个选项卡控件绑定到动态选项卡的observablecollection,如下所示:

<TabControl ItemsSource="{Binding AllTabs}" SelectedIndex="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                   <!--.............. -->
            </DataTemplate>
        </TabControl.ItemTemplate>

        <TabControl.ContentTemplate>
            <DataTemplate DataType="{x:Type vm:TabViewModel}">
                <c:MyTabItem/>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
Run Code Online (Sandbox Code Playgroud)

因此,选项卡标题和内容是动态定义的,并在可观察集合更改时分配.现在,我想隐藏一些标签而不在后面的集合中删除它们 - 以便在重新打开标签时保留数据.

理想情况下,每个聊天选项卡视图模型都有一个IsVisible属性,默认情况下设置为true.但是,我将在何处绑定此类属性以使标签项折叠?

Zen*_*uka 6

如果你可以修改你,vm:TabViewModel我应该将你的IsVisible更改为Visibility属性并使用以下ContentTemplate:

<TabControl.ContentTemplate>
    <DataTemplate DataType="{x:Type vm:TabViewModel}">
        <c:MyTabItem Visibility={Binding Visibility}/>
    </DataTemplate>
</TabControl.ContentTemplate>
Run Code Online (Sandbox Code Playgroud)

否则,您可以使用转换器将布尔值IsVisible更改为Visibility枚举:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;

namespace Something.Converters
{
    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class BoolToVisibilityConverter : IValueConverter
    {

        #region IValueConverter Members
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool && targetType == typeof(Visibility))
            {
                bool val = (bool)value;
                if (val)
                    return Visibility.Visible;
                else
                    if (parameter != null && parameter is Visibility )
                        return parameter;
                    else
                        return Visibility.Collapsed;
            }
            throw new ArgumentException("Invalid argument/return type. Expected argument: bool and return type: Visibility");
        }

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Visibility && targetType == typeof(bool))
            {
                Visibility val = (Visibility)value;
                if (val == Visibility.Visible)
                    return true;
                else
                    return false;
            }
            throw new ArgumentException("Invalid argument/return type. Expected argument: Visibility and return type: bool");
        }
        #endregion
    }
}
Run Code Online (Sandbox Code Playgroud)

在xaml中包含命名空间(您的根元素,本例中为Window):

<Window xmlns:converters="clr-namespace:Something.Converters"
.../>
Run Code Online (Sandbox Code Playgroud)

在您的资源中:

<Window.Resources>
    <converters:BoolToVisibilityConverter x:Key="boolToVisibilityConverter"/>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

最终结合:

<TabControl.ContentTemplate>
    <DataTemplate DataType="{x:Type vm:TabViewModel}">
        <c:MyTabItem Visibility={Binding IsVisible, Converter={StaticResource boolToVisibilityConverter}, ConverterParameter=Visibility.Collapsed}/>
    </DataTemplate>
</TabControl.ContentTemplate>
Run Code Online (Sandbox Code Playgroud)

我想那就是:)

编辑:Ow并将ConverterParameter更改为Visibility.Collapsed为Visibility.Hidden for hidden;)

  • 我尝试了这种确切的情况,但它只会使TabItem的内容折叠。有任何想法吗? (2认同)

小智 5

这个答案的帮助下得到了正确的答案

<TabControl.ItemContainerStyle>
  <Style TargetType="{x:Type TabItem}">
    <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource boolToVisibilityConverter}"/>
  </Style>
</TabControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)

使用System.Windows.Controls.BooleanToVisibilityConverter从bool转换为Visibilty。

Scott关于使用CollectionView的建议也很有希望。