Domanda

Ho un modello di controllo che contiene un percorso (oltre ad altri controlli).Il percorso dovrebbe essere ridimensionato quando il controllo è ridimensionato.I punti e le dimensioni che descrivono il percorso possono essere espressi come frazioni relativ della dimensione del controllo.

Ecco un estratto del modello:

<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 e End sono dipendentiProperties di tipo punto, la dimensione è una fabbrica di dipendenza della dimensione del tipo.

Quello che sto facendo attualmente sta ascoltando l'evento di 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 );
}
.

La domanda è ora: C'è un altro modo (più elegante) per legare le proprietà del percorso alle dimensioni del controllo genitore?

È stato utile?

Soluzione

Quello che hai è probabilmente il modo migliore per farlo.

Un altro modo sarebbe quello di costruire un nome personalizzato impultivaConverter che espone due proprietà pubbliche: WidtHperCettagege e AtheperCencentage.Quindi è possibile legarsi alla realtà / attualità del genitore template.

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
    }

}
.

che dovresti usare come:

<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>
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top