2013-02-20 46 views
7

我是新來創建用戶控件,現在我想自定義用戶控件模板如下:TemplateBinding如何在UserControl模板中工作?

<UserControl x:Class="WpfApplication1.PieButton" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" Loaded="OnLoaded"> 
    <UserControl.Template> 
     <ControlTemplate> 
      <Path Name="path" Stroke="Aqua" StrokeThickness="3"> 
       <Path.Fill> 
        <SolidColorBrush Color="{TemplateBinding Fill}" /> 
       </Path.Fill> 
       <Path.Data> 
       ...... 
</UserControl> 

與此同時我已創建在後端代碼的DependencyProperty:

public partial class PieButton : UserControl 
{ 
    public PieButton() 
    { 
     InitializeComponent(); 
    } 

    private void OnLoaded(object sender, RoutedEventArgs e) 
    { 

    } 



    public Color Fill 
    { 
     get { return (Color)GetValue(FillProperty); } 
     set { SetValue(FillProperty, value); } 
    } 

    public static readonly DependencyProperty FillProperty = 
     DependencyProperty.Register("Fill", typeof(Color), typeof(PieButton)); 
    ...... 

我想在XAML中使用TemplateBinding來綁定我的PieButton的Fill屬性來填充路徑對象。 Visual Studio Designer警告我「填充屬性無法訪問或識別」。

根據我的理解,TemplateBinding從應用此ControlTemplate的元素中找到屬性名稱,在此應該是PieControl,但爲什麼Fill屬性無法在此處訪問?

BTW,

我測試下面的結合,它可以爲我

Color="Binding Fill,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}" 

但我仍然認爲TemplateBinding應該能夠這種情況下工作,所以請指出我的過錯工作這裏。謝謝。

回答

相關問題