3

次のように、動的タブの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>

したがって、タブのヘッダーとコンテンツは動的に定義され、監視可能なコレクションが変更されると割り当てられます。今、いくつかのタブを後ろのコレクションで削除せずに非表示にしたいと思います - タブが再び開かれた場合にデータを保持するために。

理想的には、各チャット タブ ビューモデルには、既定で true に設定されている IsVisible プロパティがあります。ただし、タブ項目を折りたたむために、そのようなプロパティをどこにバインドすればよいでしょうか?

4

3 に答える 3

6

この答えの助けを借りて正しい答えを得ました

<TabControl.ItemContainerStyle>
  <Style TargetType="{x:Type TabItem}">
    <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource boolToVisibilityConverter}"/>
  </Style>
</TabControl.ItemContainerStyle>

boolからVisibiltyに変換するには、System.Windows.Controls.BooleanToVisibilityConverterを使用します。

CollectionViewを使用するというScottの提案も有望です。

于 2009-11-11T10:01:27.030 に答える
6

変更できる場合はvm:TabViewModel、IsVisible を Visibility プロパティに変更し、次の ContentTemplate を使用する必要があります。

<TabControl.ContentTemplate>
    <DataTemplate DataType="{x:Type vm:TabViewModel}">
        <c:MyTabItem Visibility={Binding Visibility}/>
    </DataTemplate>
</TabControl.ContentTemplate>

それ以外の場合は、コンバーターを使用してブール値の 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
    }
}

xaml に名前空間を含めます (ルート要素、この例では Window):

<Window xmlns:converters="clr-namespace:Something.Converters"
.../>

そしてあなたのリソースで:

<Window.Resources>
    <converters:BoolToVisibilityConverter x:Key="boolToVisibilityConverter"/>
</Window.Resources>

最後にバインディング:

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

それだと思います:)

編集:さて、ConverterParameterをVisibility.CollapsedからVisibility.Hiddenに変更します;)

于 2009-08-27T13:16:46.553 に答える
1

CollectionViewを使用することをお勧めします。これは、コレクションのフィルター処理された部分を見ることができるコレクションの抽象ビューのようなものです。コレクション自体ではなく CollectionView にバインドすることで、必要なものだけを表示できるようになり、コレクションはバックグラウンドに残ります。

于 2009-08-27T12:58:01.580 に答える