2013-02-20 10 views
7

私は、ユーザーコントロールを作成するには、新しいものだと今、私は以下のようにユーザーコントロールのテンプレートをカスタマイズしようとしています:UserControlテンプレートでTemplateBindingはどのように機能していますか?

<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は、「Fillプロパティにアクセスできないか認識されていません」と警告します。私の理解に基づいて

TemplateBindingはここ PieControlをする必要があります。このControlTemplateのを適用する要素からプロパティ名を見つけるが、なぜ Fillプロパティは、ここではアクセスできないのでしょうか?

ところで、

は、私は以下の結合をテストし、それは私のため

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

を働かせることができる。しかし、私はまだので、私の障害を指摘してください、TemplateBindingのはこのシナリオの下で働くことができるはずだと思いますここに。ありがとう。

答えて

関連する問題