我创建了一个自定义GroupBox控件,它在右上角提供了一个按钮。我注意到边框没有像标题那样为按钮提供间隙,所以我写了一个自定义BorderGapConverter
。一切都有效,直到标题的内容发生变化。边框,我相信在调整包含控件的窗口大小之前,opacitymask不会改变。
ButtonedGroupBox.cs
namespace My.Controls
{
/// <summary>
/// Interaction logic for ButtonedGroupBox.xaml
/// </summary>
public partial class ButtonedGroupBox : GroupBox
{
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ButtonedGroupBox), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ButtonedGroupBox), new FrameworkPropertyMetadata(null));
protected override void OnHeaderChanged(object oldHeader, object newHeader)
{
base.OnHeaderChanged(oldHeader, newHeader);
try
{
GetBindingExpression(OpacityMaskProperty).UpdateTarget();
}
catch (NullReferenceException) { }
}
public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } }
public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } }
public ButtonedGroupBox()
{
InitializeComponent();
}
}
}
ButtonedGroupBox.xaml
<GroupBox x:Class="My.Controls.ButtonedGroupBox" x:Name="ctrlThis"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cv="clr-namespace:My.ValueConverters"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<GroupBox.Resources>
<BorderGapMaskConverter x:Key="BorderGapMaskConverter"/>
<cv:BorderGapConverter x:Key="MyBorderGapMaskConverter"/>
</GroupBox.Resources>
<GroupBox.Style>
<Style TargetType="{x:Type GroupBox}">
<Setter Property="BorderBrush" Value="#D5DFE5"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
</GroupBox.Style>
<GroupBox.Template>
<ControlTemplate TargetType="{x:Type GroupBox}">
<Grid SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="6"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="6"/>
</Grid.RowDefinitions>
<Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="5" Grid.Column="0" CornerRadius="4" Grid.Row="1" Grid.RowSpan="3"/>
<Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="5" CornerRadius="4" Grid.Row="1" Grid.RowSpan="3">
<Border.OpacityMask>
<MultiBinding ConverterParameter="6" Converter="{StaticResource MyBorderGapMaskConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Grid}"/>
<Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</Border.OpacityMask>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
<Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/>
</Border>
</Border>
<Border x:Name="Header" Grid.Column="1" Padding="3,1,3,1" Grid.Row="0" Grid.RowSpan="2">
<ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<Border Grid.Column="3" Padding="3,1,3,1" Grid.Row="0" Grid.RowSpan="2">
<Button x:Name="CopyButton" Content="Copy" Grid.Column="3" Grid.Row="0" Grid.RowSpan="2" HorizontalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Command="{Binding Command, ElementName=ctrlThis}" CommandParameter="{Binding CommandParameter, ElementName=ctrlThis}"/>
</Border>
<ContentPresenter Grid.ColumnSpan="3" Grid.Column="1" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</ControlTemplate>
</GroupBox.Template>
</GroupBox>
BorderGapConverter
public class BorderGapConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Type doubleType = typeof(double);
// Data Validation
if (values == null) return DependencyProperty.UnsetValue;
foreach (var value in values)
{
Type valueType = value.GetType();
if (value == null || (!typeof(FrameworkElement).IsAssignableFrom(valueType) && !doubleType.IsAssignableFrom(valueType)))
return DependencyProperty.UnsetValue;
}
Type paramType = parameter.GetType();;
if (parameter == null || (!doubleType.IsAssignableFrom(paramType) && !typeof(string).IsAssignableFrom(paramType))) return DependencyProperty.UnsetValue;
double offset = 0d;
if (parameter is string)
offset = double.Parse((string)parameter, NumberFormatInfo.InvariantInfo);
else
offset = (double)parameter;
Grid hostGrid = (Grid)values[0];
double borderWidth = (double)values[1];
double borderHeight = (double)values[2];
if (borderWidth == 0d || borderHeight == 0d) return null;
// Create a 5x2 Grid
Grid grid = new Grid() { Width = borderWidth, Height = borderHeight };
foreach(ColumnDefinition col in hostGrid.ColumnDefinitions)
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(col.ActualWidth) });
// Define rows
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(borderHeight / 2d) });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1d, GridUnitType.Star) });
/**
* Cartesian Coordinates
* - 0 1 2
* 0|-|-|-|
* 1|-|-|-|
*/
// Set rectangle 1 to 0, 0 to 0, 1
Rectangle rectangle = new Rectangle() { Fill = Brushes.Black };
Grid.SetRowSpan(rectangle, 2);
Grid.SetRow(rectangle, 0);
Grid.SetColumn(rectangle, 0);
// Set rectangle2 to 1, 1
Rectangle rectangle2 = new Rectangle() { Fill = Brushes.Black };
Grid.SetRow(rectangle2, 1);
Grid.SetColumn(rectangle2, 1);
// Set rectangle3 to 0, 2 to 1, 2
Rectangle rectangle3 = new Rectangle() { Fill = Brushes.Black };
Grid.SetRowSpan(rectangle3, 2);
Grid.SetRow(rectangle3, 0);
Grid.SetColumn(rectangle3, 2);
// Set rectangle4 to 0, 2 to 1, 2
Rectangle rectangle4 = new Rectangle() { Fill = Brushes.Black };
Grid.SetRow(rectangle4, 1);
Grid.SetColumn(rectangle4, 3);
// Set rectangle3 to 0, 2 to 1, 2
Rectangle rectangle5 = new Rectangle() { Fill = Brushes.Black };
Grid.SetRowSpan(rectangle5, 2);
Grid.SetRow(rectangle5, 0);
Grid.SetColumn(rectangle5, 4);
grid.Children.Add(rectangle);
grid.Children.Add(rectangle2);
grid.Children.Add(rectangle3);
grid.Children.Add(rectangle4);
grid.Children.Add(rectangle5);
return new VisualBrush(grid);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return new object[] { Binding.DoNothing };
}
}
我尝试覆盖HeaderProperty
上的元数据,覆盖OnHeaderChanged
,更改为使用HeaderTemplate
,并绑定Header
并设置HeaderStringFormat
无济于事。
以下是我使用控件的方式:
<ctrl:ButtonedGroupBox Command="{Binding CopyContentsCommand}" Header="{Binding DynamicTitle}" HeaderStringFormat="Static - {0}">
<TextBlock>
Lorem ipsum sit dolorem
</TextBlock>
</ctrl:ButtonedGroupBox>
当标题的基础文本块发生变化时,如何让opacitymask更新?
修改
所以我将Header元素的ActualWidth添加到MultiBinding中,工作但似乎没必要。
<MultiBinding ConverterParameter="6" Converter="{StaticResource MyBorderGapMaskConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Grid}"/>
<Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
<Binding ElementName="Header" Path="ActualWidth"/>
</MultiBinding>