0
我想使其與2011年之前的問題相同: Should binding to Path.Data property work?但我的問題是我有任何其他錯誤,我不明白巫婆之一。我繼電器因爲視圖天我在做什麼錯誤的期待,但我必須是盲人......在用戶控件的Path.Data屬性上進行綁定
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()));
}
主窗口.xaml:
<Grid>
<ctl:CustomControl x:Name="Curve"/>
</Grid>
就是這樣。主Window.xaml.cs只有他的構造函數。但我真的不知道問題是什麼。 :(
綁定源的臨時實例只有幾千一個設法得到它工作。但是我也嘗試從MainWindow.xaml.cs中調用構建函數,並且此時它也從相同的實例調用,不是嗎? – Teroman
看我的編輯。 Build現在是一個公共實例方法,可以在CustomControl實例上調用,例如在MainWindow的代碼後面。因此你可以保存實例構造函數。 – Clemens