2012-04-20 38 views
6

मेरे पास मेरा स्वयं का UserControl है, एक LabeledTextBox जो Label और एक..वेल, TextBox का संयोजन है। इस नियंत्रण में दो गुण हैं: Caption जो Label, और Value के कैप्शन के लिए बाध्य होंगे जो TextBox के Text से बाध्य होंगे।उपयोगकर्ता नियंत्रण के भीतर से दोवे बाध्यकारी का उपयोग कैसे करें?

कोड:

public class LabeledTextBox : Control 
{ 
    static LabeledTextBox() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledTextBox), new FrameworkPropertyMetadata(typeof(LabeledTextBox))); 
    } 

    public string Caption 
    { 
     get { return (string)GetValue(CaptionProperty); } 
     set { SetValue(CaptionProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Caption. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty CaptionProperty = 
     DependencyProperty.Register("Caption", typeof(string), typeof(LabeledTextBox), new UIPropertyMetadata("")); 


    public string Value 
    { 
     get { return (string)GetValue(ValueProperty); } 
     set { SetValue(ValueProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ValueProperty = 
     DependencyProperty.Register("Value", typeof(string), typeof(LabeledTextBox), new UIPropertyMetadata("")); 


} 

XAML:

<Style TargetType="{x:Type local:LabeledTextBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:LabeledTextBox}"> 
       <Grid> 
        <Grid> 

        <Grid.RowDefinitions> 
         <RowDefinition /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 

        <Label Grid.Row="0" Content="{TemplateBinding Caption}" /> 
        <TextBox Name="Box" Margin="3,0,3,3" Grid.Row="1" Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" /> 

        </Grid> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

उपयोग:

<uc:LabeledTextBox Caption="Code:" Value="{Binding ExpenseCode}" /> 

शुरू में मैंने सोचा था कि मैं अपने जवाब यहाँ मिल गया था: WPF TemplateBinding vs RelativeSource TemplatedParent

कि एडवेंचर्स विवरण TemplateBinding और RelativeSource TemplatedParent के बीच सीई। मैंने तदनुसार अपना कोड बदल दिया है, लेकिन ऐसा लगता है कि मुझे एक कदम याद आ रहा है। वनवे बाध्यकारी काम करता है, मेरा टेक्स्टबॉक्स वैल्यू प्रॉपर्टी से जुड़ा हुआ है, लेकिन परिवर्तन पंजीकृत नहीं होते हैं।

मैं इसे काम करने के लिए कैसे प्राप्त करूं?

+0

क्या आपने अपनी वैल्यू निर्भरता संपत्ति दोवे बनाने की कोशिश की है? या यह समस्या है कि टेक्स्टबॉक्स आपके वैल्यू डीपी में नहीं लिखता है? – dowhilefor

+0

क्या यह 'लेबल किया गया टेक्स्टबॉक्स' वैल्यू 'है जो डेटा ऑब्जेक्ट में नहीं बदल रहा है या यह' ExpenseCode' है? आप एक 'PropertyChangedCallback] (http://msdn.microsoft.com/en-us/library/system.windows.propertychangedcallback.aspx) को' वैल्यू 'निर्भरता प्रॉपर्टी में देख सकते हैं कि यह कहां जाता है या नहीं। – Clemens

+0

@dowhilefor: क्या यह नहीं है कि एक्सएएमएल में 'मोड = टूवे' क्या है? @ क्लेमेन: मैंने शुरुआत में उस कॉलबैक को किया था, और इसे बुलाया गया था। उस कॉलबैक में मैंने टेक्स्टबॉक्स का मान सेट किया था। क्या मुझे डेटा को डीपी के भीतर से स्पष्ट रूप से वापस करने की आवश्यकता है? @Ricibob: धन्यवाद, लेकिन ऐसा लगता है कि यह मामला नहीं है। – diggingforfire

उत्तर

6

यहां मोड बदलें।

<uc:LabeledTextBox Caption="Code:" Value="{Binding ExpenseCode,Mode=TwoWay}" /> 

यह शायद ज़रुरत किसी को भी मेरी अंत

+0

ग्रेट, यह चाल है! – diggingforfire

+1

मैंने घोषणा एन शैली में मोड के सभी संभावित संयोजनों की कोशिश की। @dowhile सही है, व्याख्या के लिए धन्यवाद :) – Akanksha

+0

मैं अभी तक एक WPF adept नहीं हूँ, लेकिन यह एक अच्छा तरीका है :) – diggingforfire

4

में काम किया यह समस्या है:

एक और दृष्टिकोण (शायद और अधिक सुरुचिपूर्ण) ताकि एक तरह से usercontrol की निर्भरता संपत्ति घोषित करने के लिए किया जाएगा यह बाध्यकारी दो तरीकों से चूक जाता है (उदाहरण के लिए फ्रेमवर्क टेक्स्टबॉक्स डिफ़ॉल्ट रूप से करता है)।

यह इस प्रकार प्राप्त किया जा सकता (this Stackoverflow question के जवाब से लिया गया):

public DependencyProperty SomeProperty = 
     DependencyProperty.Register("Some", typeof(bool), typeof(Window1), 
      new FrameworkPropertyMetadata(default(bool), 
       FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 

कुंजी यहाँ FrameworkPropertyMetadata उपयोग कर रहा है।