2011-08-14 13 views
13

कोड के पहले संस्करण (MainWindow.xaml) पर विचार करें:कुछ नियंत्रण के लिए टेम्पलेट के भाग के रूप ScrollViewer उपयोग करते समय, लेफ्ट क्लिक नियंत्रित किया जाता है

<ScrollViewer> 
    <local:CustomControl1 Width="1000" Height="1000" Background="Red"/> 
</ScrollViewer> 

जहां CustomControl1 ItemsControl से ली गई। कस्टमकंट्रोल 1 के अंदर, मैं ऑनमाउसडाउन घटना को ओवरराइड कर रहा हूं। यह कोड पूरी तरह से काम करता है, और मैं माउस नीचे घटना पकड़ो।

कोड का अब दूसरा संस्करण (MainWindow.xaml):

<local:CustomControl1 Width="1000" Height="1000" Background="Red"/> 

अंदर Generic.xaml, मैं अपने आइटम के टेम्पलेट बदलते को नियंत्रित:

  <ControlTemplate TargetType="{x:Type local:CustomControl1}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}"> 

        <ScrollViewer 
         VerticalScrollBarVisibility="Visible" 
         HorizontalScrollBarVisibility="Visible" 
         > 
         <ItemsPresenter/> 
        </ScrollViewer> 
       </Border> 
      </ControlTemplate> 

जब मैं नियंत्रण के हिस्से के रूप ScrollViewer डाल टेम्पलेट, मैं अब OnMouseDownEvent प्राप्त नहीं कर रहा हूं (बाएं क्लिक के लिए)। किसी कारण से, अब माउस डाउन इवेंट को संभालने के रूप में चिह्नित किया गया है। अगर मैं निम्नलिखित बयान उपयोग करने के बजाय OnMouseDown अधिभावी के आइटम निर्माता को नियंत्रित अंदर, घटना catched है:

AddHandler(Mouse.MouseDownEvent, new MouseButtonEventHandler(OnMouseDown), true); 

पहले, मैं समझने के लिए क्यों माउस के व्यवहार नीचे बदलते टेम्पलेट के अंदर ScrollViewer रखने चाहते हैं। दूसरा, क्या किसी को कुछ कामकाज पता है? मैंने प्रस्तावित समाधान (हैंडल की घटनाओं को पकड़कर) मेरे लिए स्वीकार्य नहीं है। मेरे आवेदन में, मुझे माउस डाउन इवेंट्स को केवल तभी संभालना होगा जब कोई भी आइटम बच्चों को नियंत्रित नहीं करता है।

अग्रिम धन्यवाद।

उत्तर

21

आप उदाहरण के लिए एक ListBox (जो भी ItemsControl से निकला है) के लिए डिफ़ॉल्ट Template को देखें, तो आप ScrollViewer टेम्पलेट में Focusable="False" सेट है कि देखेंगे। यह माउस घटनाओं को आपके नियंत्रण में

<ScrollViewer VerticalScrollBarVisibility="Visible" 
       HorizontalScrollBarVisibility="Visible" 
       Focusable="False" > 
    <ItemsPresenter/> 
</ScrollViewer> 
+0

अच्छा पकड़, बहुत बहुत धन्यवाद! – Illidan

+0

मुझे समझ में नहीं आता है। फोकस करने योग्य = गलत प्रभाव घटना प्रबंधन कैसे करता है? –