テンプレートバインディングを介してパスプロパティを変更します
-
07-09-2020 - |
質問
パスを含むコントロールテンプレート(他のコントロールの他に)。コントロールのサイズ変更時にパスをサイズ変更する必要があります。パスを記述するポイントとサイズは、コントロールサイズのRelativフラクションとして表現できます。
これはテンプレートの抜粋です:
<Path Stroke="Gray" StrokeThickness="5">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="{TemplateBinding Start}" >
<ArcSegment Point="{TemplateBinding End}" Size="{TemplateBinding Size}" RotationAngle="0" IsLargeArc="True" />
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
.
STARTとENDはタイプポイントの依存性プロパティです。サイズはタイプサイズの依存関係プロテリアです。
現在やっているのはFrameworkElement.SizeChangedイベントを聞いています:
void OperationModeIndicator_SizeChanged( object sender, SizeChangedEventArgs e )
{
this.Size = new Size( e.NewSize.Width * 0.45f, e.NewSize.Height * 0.45f );
this.Start = new Point( e.NewSize.Width * 0.35f, e.NewSize.Height * 0.1f );
this.End = new Point( e.NewSize.Width * 0.65f, e.NewSize.Height * 0.1f );
}
.
問題は次のとおりです。 パスのプロパティを親コントロールのサイズにバインドする別の(よりエレガントな)方法はありますか?
解決
あなたが持っているものはおそらくこれを達成するための最良の方法です。
もう1つの方法は、カスタム IMultivalueConverter WidthperCentageとHightperCentageの2つのパブリックプロパティを公開します。その後、テンプレート化された親の実際のwidth / ActualHeightにバインドできます。
public class MyConverter : IMultiValueConverter {
public double HeightPercentage { get; set; }
public double WidthPercentage { get; set; }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
// TODO: Validate values size and types
return new Point(((double)values[0]) * this.WidthPercentage, ((double)values[1]) * this.HeightPercentage);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
// No-op or throw
}
}
.
<local:MyConverter x:Key="StartPointConverter"
WidthPercentage="0.35" HeightPercentage="0.1" />
<!-- ... -->
<PathFigure>
<PathFigure.StartPoint>
<MultiBinding Converter="{StaticResource StartPointConverter}">
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ActualWidth" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ActualHeight" />
</MultiBinding>
</PathFigure.StartPoint>
<!-- ... -->
</PathFigure>
. 所属していません StackOverflow