Ich möchte das gleiche wie in einer früheren Frage aus dem Jahr 2011 machen: Should binding to Path.Data property work? aber mein Problem ist, dass ich einen anderen Fehler habe und ich verstehe nicht eine Hexe. Ich bin Relais seit ein Blick Tage suchen, was ich falsch mache, aber ich muss blind sein ...Binding auf Path.Data Eigenschaft bei einem UserControl
generic.xaml:
<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Path Data="{Binding GeometryData}" x:Name="CurveGraph" Stroke = "Black" StrokeThickness = "2" Grid.RowSpan="4"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
CustomControl.cs:
public class CustomControl : Control
{
static CustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
CustomControl tmp = new CustomControl();
tmp.Build();
}
public GeometryGroup Build()
{
var curve = new GeometryGroup();
curve.Children.Add(new LineGeometry(new Point(0, 0), new Point(20, 20)));
curve.Children.Add(new LineGeometry(new Point(0, 20), new Point(20, 0)));
new CustomControl().GeometryData = curve;
return curve;
}
private GeometryGroup GeometryData
{
get { return (GeometryGroup)GetValue(GeometryDataProperty); }
set { SetValue(GeometryDataProperty, value); }
public static readonly DependencyProperty GeometryDataProperty = DependencyProperty.Register("GeometryData", typeof(GeometryGroup), typeof(CustomControl), new UIPropertyMetadata(new GeometryGroup()));
}
Mainwindow .xaml:
<Grid>
<ctl:CustomControl x:Name="Curve"/>
</Grid>
Das ist es. Das Hauptfenster.xaml.cs hat nur seinen Konstruktor. Aber ich weiß wirklich nicht, was das Problem ist. :(
Die temporäre Instanz war nur einer von Tausenden versuchen, es arbeiten zu erhalten. Aber ich habe auch versucht, die Build-Funktion von der MainWindow.xaml.cs zu nennen, und an diesem Punkt wird es auch von den gleichen Augenblicken aufgerufen, nicht wahr? – Teroman
Siehe meine Bearbeitung. Build ist jetzt eine öffentliche Instanzmethode, die für eine CustomControl-Instanz aufgerufen werden kann, z. im MainWindow-Code dahinter. Daher könnten Sie den Instanzkonstruktor wahrscheinlich speichern. – Clemens