我可能没有在我的继承财产上设置正确的元数据选项!
当我设法让它工作时调整它们!
为了清楚起见,我将在下面发布我可以写的最简单的示例(我以前没有这样做,因为即使是最简单的示例也很长!!)
Size123Control.cs
public class Size123Control : Control
{
static Size123Control()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Size123Control), new FrameworkPropertyMetadata(typeof(Size123Control)));
}
}
Size123Panel.cs
public class Size123Panel : Panel
{
public static int GetSize123(DependencyObject obj) { return (int)obj.GetValue(Size123Property); }
public static void SetSize123(DependencyObject obj, int value) { obj.SetValue(Size123Property, value); }
public static readonly DependencyProperty Size123Property = DependencyProperty.RegisterAttached(
"Size123", typeof(int), typeof(Size123Panel),
new FrameworkPropertyMetadata(3,
FrameworkPropertyMetadataOptions.Inherits
| FrameworkPropertyMetadataOptions.AffectsMeasure
| FrameworkPropertyMetadataOptions.AffectsParentMeasure
| FrameworkPropertyMetadataOptions.AffectsArrange
| FrameworkPropertyMetadataOptions.AffectsParentArrange
));
protected override Size ArrangeOverride(Size finalSize)
{
double hTot = 0, h = 0, x = 0, xMax = 0;
foreach (UIElement child in Children)
{
var cs = child.DesiredSize;
if (x > 0 && x + cs.Width > finalSize.Width)
{
hTot += h;
x = 0;
h = cs.Height;
}
else
{
if (cs.Height > h)
h = cs.Height;
}
child.Arrange(new Rect(new Point(x, hTot), cs));
x += cs.Width;
if (x > xMax)
xMax = x;
}
return new Size(xMax, hTot + h);
}
protected override Size MeasureOverride(Size availableSize)
{
Func<Size> measure = () =>
{
double hTot = 0, h = 0, x = 0, xMax = 0;
foreach (UIElement child in Children)
{
child.Measure(availableSize);
var cs = child.DesiredSize;
if (x > 0 && x + cs.Width > availableSize.Width)
{
hTot += h;
x = 0;
h = cs.Height;
}
else
{
if (cs.Height > h)
h = cs.Height;
}
x += cs.Width;
if (x > xMax)
xMax = x;
}
return new Size(xMax, hTot + h);
};
SetSize123(this, 3);
var s1 = measure();
if (s1.Width <= availableSize.Width && s1.Height <= availableSize.Height)
return s1;
SetSize123(this, 2);
var s2 = measure();
if (s2.Width <= availableSize.Width && s2.Height <= availableSize.Height)
return s2;
SetSize123(this, 1);
var s3 = measure();
return s3;
}
}
generic.xaml
<Style TargetType="{x:Type local:Size123Control}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Size123Control}">
<Border x:Name="MAIN" Width="48" Height="48" Background="Red" BorderThickness="1" BorderBrush="LightGray"/>
<ControlTemplate.Triggers>
<Trigger Property="local:Size123Panel.Size123" Value="2">
<Setter TargetName="MAIN" Property="Background" Value="Green"/>
<Setter TargetName="MAIN" Property="Width" Value="32"/>
<Setter TargetName="MAIN" Property="Height" Value="32"/>
</Trigger>
<Trigger Property="local:Size123Panel.Size123" Value="1">
<Setter TargetName="MAIN" Property="Background" Value="Blue"/>
<Setter TargetName="MAIN" Property="Width" Value="16"/>
<Setter TargetName="MAIN" Property="Height" Value="16"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
main.xaml
<local:Size123Panel>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
<local:Size123Control/>
</local:Size123Panel>