Rya*_*saw 48
我拿了乔丹的例子并做了一些修改.此版本适用于任意数量的选项卡:
namespace WpfApplication1.Converters
{
public class TabSizeConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
TabControl tabControl = values[0] as TabControl;
double width = tabControl.ActualWidth / tabControl.Items.Count;
//Subtract 1, otherwise we could overflow to two rows.
return (width <= 1) ? 0 : (width - 1);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
xaml中的相同命名空间:
xmlns:local="clr-namespace:WpfApplication1.Converters"
Run Code Online (Sandbox Code Playgroud)
这将使所有标签使用它:
<Window.Resources>
<local:TabSizeConverter x:Key="tabSizeConverter" />
<Style TargetType="{x:Type TabItem}">
<Setter Property="Width">
<Setter.Value>
<MultiBinding Converter="{StaticResource tabSizeConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor,
AncestorType={x:Type TabControl}}" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor,
AncestorType={x:Type TabControl}}" Path="ActualWidth" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
Cha*_*lie 18
每个人似乎都在转换路径,但它实际上就像在模板中UniformGrid使用Rows设置为1 一样简单,而不是.当然,你必须重新模板化,但这并不算太糟糕.TabControlTabPanel
小智 6
我能够使用像这样的转换器:
namespace WpfApplication1.Converters
{
public class SizeConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
double width = Double.Parse(value.ToString());
//Subtract 1, otherwise we could overflow to two rows.
return .25 * width - 1;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
然后将命名空间添加到我的xaml:
xmlns:local="clr-namespace:WpfApplication1.Converters"
Run Code Online (Sandbox Code Playgroud)
然后使所有TabItems使用转换器:
<Window.Resources>
<local:SizeConverter x:Key="sizeConverter" />
<Style TargetType="{x:Type TabItem}">
<Setter Property="Width" Value="{Binding ElementName=x_Grid, Path=ActualWidth, Converter={StaticResource sizeConverter}}" />
</Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
x_Grid是x:父元素的名称我希望标签是1/4,如果这是有道理的.
我是一个老派风格的人。并且更喜欢将这种功能封装到控件本身的代码中。我的派生控件如下所示:
public class CustomTabControl :TabControl
{
protected override void OnRenderSizeChanged(System.Windows.SizeChangedInfo sizeInfo)
{
foreach (TabItem item in this.Items)
{
double newW = (this.ActualWidth / Items.Count) - 1;
if (newW < 0) newW = 0;
item.Width = newW;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的 XAML 看起来像
</infrastructure:CustomTabControl>
<TabItem />
<TabItem />
</infrustracture:CustomControl>
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么每个人都更喜欢样式控制而不是派生。
| 归档时间: |
|
| 查看次数: |
40840 次 |
| 最近记录: |