如果你可以修改你vm:TabViewModel
我要你的可见性更改为可见性属性,并使用下面的ContentTemplate:
<TabControl.ContentTemplate>
<DataTemplate DataType="{x:Type vm:TabViewModel}">
<c:MyTabItem Visibility={Binding Visibility}/>
</DataTemplate>
</TabControl.ContentTemplate>
否则,你可以使用一个转换为布尔ISVISIBLE更改为能见度枚举:
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
}
}
Inculde在XAML命名空间(您的根元素,窗口在本示例):
<Window xmlns:converters="clr-namespace:Something.Converters"
.../>
而在你的资源:
<Window.Resources>
<converters:BoolToVisibilityConverter x:Key="boolToVisibilityConverter"/>
</Window.Resources>
而且finaly绑定:
<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能见度。隐藏起来;)
完美运作!谢谢:) – bluebit 2009-08-28 08:40:45
我试过这种确切的情况,但它只是使TabItem的内容崩溃。有任何想法吗? – Jordan 2014-01-22 21:08:54