2013-02-20 53 views
7

मैं UserControl बनाने के लिए नए से एक हूं और अब मैं नीचे के रूप में UserControl खाका अनुकूलित करने के लिए कोशिश कर रहा हूँ:UserControl टेम्पलेट में टेम्पलेट बाइंडिंग कैसे काम कर रहा है?

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)); 
    ...... 
: एक ही समय मैं बैक-एंड कोड में DependencyProperty बनाने पर

<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> 

मैं पथ वस्तु को भरने के लिए Fill मेरी पाईबटन की संपत्ति को बांधने के लिए XAML में TemplateBinding का उपयोग करना चाहता हूं। विजुअल स्टूडियो डिजाइनर ने मुझे चेतावनी दी है कि "भरें संपत्ति सुलभ या मान्यता प्राप्त नहीं है"।

मेरी समझ के आधार पर, TemplateBinding तत्व यह है कि इस ControlTemplate लागू होते हैं, जो PieControl यहाँ होना चाहिए से संपत्ति नाम मिल जाए, लेकिन क्यों Fill संपत्ति यहाँ तक नहीं पहुँच सकते हैं?

BTW

,

मैं बाध्यकारी निम्नलिखित का परीक्षण, और यह मेरे

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

लेकिन मैं अभी भी लगता है कि TemplateBinding इस परिदृश्य के तहत काम करने के लिए है, इसलिए कृपया मेरी गलती का कहना है सक्षम होना चाहिए के लिए काम कर सकते हैं यहाँ। धन्यवाद।

उत्तर

16

TemplateBinding to DependencyProperty on a custom control is not working के अनुसार, टेम्पलेट बाइंडिंग नियंत्रण पर कस्टम निर्भरता गुणों के लिए काम नहीं करती है।

समाधान के रूप में इसका इस्तेमाल करने के

{Binding MyProperty, RelativeSource={RelativeSource TemplatedParent}}

+0

आप इस के लिए धन्यवाद का सुझाव दिया है। – m4design

संबंधित मुद्दे