2013-01-15 27 views
6

के साथ एक MulitBinding का उपयोग करें मैं WPF में एक कस्टम नियंत्रण कर रहा हूँ। मैं अभी भी एक टेम्पलेट बाइंडिंग (कस्टम नियंत्रण में बहुत उपयोग किया जाता है) के इन-एंड-आउट सीख रहा हूं।टेम्पलेट बाइंडिंग

एक सोचता है कि मुझे लगता है कि मैं एक MulitBinding के अंदर टेम्पलेट बाइंडिंग का उपयोग नहीं कर सकता।

जब मैं इस प्रयास करें:

The value "System.Windows.TemplateBindingExpression" is not of type "System.Windows.Data.BindingBase" and cannot be used in this generic collection.
Parameter name: value

मैं कुछ याद आ रही हूँ:

<ComboBox.ItemsSource> 
    <MultiBinding Converter="{StaticResource MyMultiConverter}"> 
     <Binding ElementName="PART_AComboBox" Path="SelectedItem"/> 
     <TemplateBinding Property="MyListOne"/> 
     <TemplateBinding Property="MyListTwo"/> 
    </MultiBinding> 
</ComboBox.ItemsSource> 

मैं इस त्रुटि मिलती है? क्या यह काम करने का कोई तरीका है?

इस तरीके को मैं जा रहा है, लेकिन यह एक हैक की तरह है:

<ListBox x:Name="ListOne" 
     ItemsSource="{TemplateBinding MyListOne}" 
     Visibility="Collapsed" /> 
<ListBox x:Name="ListTwo" 
     ItemsSource="{TemplateBinding MyListTwo}" 
     Visibility="Collapsed" /> 

<ComboBox.ItemsSource> 
    <MultiBinding Converter="{StaticResource DictionaryFilteredToKeysConverter}"> 
     <Binding ElementName="PART_TextTemplateAreasHost" Path="SelectedItem"/> 
     <Binding ElementName="ListOne" Path="ItemsSource"/> 
     <Binding ElementName="ListTwo" Path="ItemsSource"/> 
    </MultiBinding> 
</ComboBox.ItemsSource> 

मैं निर्भरता संपत्ति को listboxes बाँध और फिर मेरी mulitbinding में मैं के ItemsSource के लिए एक तत्व बाँध कर सूची बक्से

जैसा कि मैंने उपरोक्त कहा है, यह एक हैक और जैसा लगता है, मैं जानना चाहता हूं कि टेम्पलेट बाइंडिंग के साथ मल्टी बाइंडिंग करने के लिए कोई सही तरीका है या नहीं।

उत्तर

16

आप इस्तेमाल कर सकते हैं:

<Binding Path="MyListOne" RelativeSource="{RelativeSource TemplatedParent}"/> 

TemplateBinding वास्तव में सिर्फ एक छोटी हाथ, अनुकूलित ऊपर का संस्करण है। यह बहुत सख्त है कि इसका उपयोग कैसे किया जा सकता है (सीधे पदानुक्रमित पथ और cetera के बिना टेम्पलेट के अंदर)।

एक्सएएमएल कंपाइलर अभी भी इस तरह के मुद्दों के बारे में सभ्य प्रतिक्रिया देने पर बहुत कचरा है (कम से कम 4.0 में, विशेष रूप से 4.5 के लिए परीक्षण नहीं किया गया है)।

<ControlTemplate TargetType="..."> 
    <Path ...> 
     <Path.RenderTransform> 
      <RotateTransform Angle="{TemplateBinding Tag}"/> 
     </Path.RenderTransform> 
    </Path> 
</ControlTemplate> 

यह संकलित और ठीक से क्रियान्वित लेकिन रोटेशन कोण को Tag में मूल्य बाध्यकारी नहीं था: मैं बस अब इस XAML था। मैंने snooped और देखा कि संपत्ति बाध्य थी, लेकिन शून्य। सहजता से (इस त्रासदी से निपटने के वर्षों के बाद) मैंने इसे बदल दिया:

<ControlTemplate TargetType="..."> 
    <Path ...> 
     <Path.RenderTransform> 
      <RotateTransform Angle="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/> 
     </Path.RenderTransform> 
    </Path> 
</ControlTemplate> 

और यह ठीक काम किया।

+0

बिल्कुल सही! महान जवाब के लिए धन्यवाद! – Vaccano