2016-04-26 57 views
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只有他的構造函數。但我真的不知道問題是什麼。 :(

回答

0

這是沒有意義的(靜態)類的構造函數創建一個臨時CustomControl實例,並調用生成方法,這臺GeometryData上又臨時實例。

相反,添加實例構造函數在初始化當前實例GeometryData。另外,還要GeometryData公開。

public class CustomControl : Control 
{ 
    public static readonly DependencyProperty GeometryDataProperty = 
     DependencyProperty.Register(
      "GeometryData", typeof(GeometryGroup), typeof(CustomControl)); 

    public GeometryGroup GeometryData 
    { 
     get { return (GeometryGroup)GetValue(GeometryDataProperty); } 
     set { SetValue(GeometryDataProperty, value); } 
    } 

    static CustomControl() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), 
      new FrameworkPropertyMetadata(typeof(CustomControl))); 
    } 

    public CustomControl() 
    { 
     Build(); 
    } 

    public void 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))); 
     GeometryData = curve; 
    } 
} 

除此之外,你要麼使用在ControlTemplate中

一個

或指定一個像

<Path Data="{Binding GeometryData, RelativeSource={RelativeSource TemplatedParent}}" ... /> 
+0

綁定源的臨時實例只有幾千一個設法得到它工作。但是我也嘗試從MainWindow.xaml.cs中調用構建函數,並且此時它也從相同的實例調用,不是嗎? – Teroman

+0

看我的編輯。 Build現在是一個公共實例方法,可以在CustomControl實例上調用,例如在MainWindow的代碼後面。因此你可以保存實例構造函數。 – Clemens

相關問題