2011-10-13 17 views
16

मेरे पास एक शैली है, और मैं EventSetter के Handler पर RelativeSource के साथ एक आदेश बांधना चाहता हूं। कमांड दृश्य में है।शैली में बाध्यकारी wpf eventetter हैंडलर

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}"> 
    <EventSetter Event="MouseLeftButtonDown" 
       Handler="{Binding TextBlockMouseLeftButtonDownCommand, 
          RelativeSource={RelativeSource Self}}"/> 
</Style> 
समस्या

है कि मैं कोई त्रुटि मिलती है, क्योंकि कुछ इस

मैंने पहले एक बहुत googled है (शायद यह इस तरह आसान तरीका में यह करने के लिए संभव नहीं है), और मैंने पाया के साथ गलत है AttachedCommandBehaviour, लेकिन मुझे लगता है कि यह शैली के साथ काम नहीं करता है।

क्या आप इस समस्या को हल करने के बारे में कुछ संकेत दे सकते हैं?

अद्यतन 13/10/2011

मैं MVVM लाइट टूलकिट EventToCommand उदाहरण कार्यक्रम में यह पाया:

 <Button Background="{Binding Brushes.Brush1}" 
      Margin="10" 
      Style="{StaticResource ButtonStyle}" 
      Content="Simple Command" 
      Grid.Row="1" 
      ToolTipService.ToolTip="Click to activate command"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Click"> 
       <cmd:EventToCommand Command="{Binding SimpleCommand}" /> 
      </i:EventTrigger> 
      <i:EventTrigger EventName="MouseLeave"> 
       <cmd:EventToCommand Command="{Binding ResetCommand}" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </Button> 

लेकिन यहाँ, बाध्यकारी शैली में नहीं है। मैं इस EventToCommand बटन की शैली में कैसे रख सकता हूं?

उत्तर

21

सुझाव है अभी आप TextBlock.TextBlockMouseLeftButtonDownCommand करने के लिए MouseLeftButtonDown घटना बाध्यकारी हैं। TextBlockMouseLeftButtonDownCommand टेक्स्टब्लॉक के लिए मान्य संपत्ति नहीं है, और न ही यह एक इवेंट हैंडलर जैसा लगता है।

मैं किसी ईवेंट को कमांड करने के लिए शैलियों में AttachedCommandBehavior का उपयोग करता हूं। वाक्य रचना आम तौर पर लगता है कि यह (कमांड में DataContext ध्यान दें बाइंडिंग) की तरह:

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}"> 
    <Setter Property="local:CommandBehavior.Event" Value="MouseLeftButtonDown" /> 
    <Setter Property="local:CommandBehavior.Command" 
      Value="{Binding DataContext.TextBlockMouseLeftButtonDownCommand, 
          RelativeSource={RelativeSource Self}}" /> 
</Style> 

विकल्प में एक घटना कोड-पीछे करने के लिए EventSetter हुक करने के लिए है, और वहाँ से आदेश पर कार्रवाई:

पीछे कोड में
<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}"> 
    <EventSetter Event="MouseLeftButtonDown" 
       Handler="TextBlockMouseLeftButtonDown"/> 
</Style> 

ईवेंट हैंडलर ...

void TextBlockMouseLeftButtonDown(object sender, MouseEventArgs e) 
{ 
    var tb = sender as TextBlock; 
    if (tb != null) 
    { 
     MyViewModel vm = tb.DataContext as MyViewModel; 

     if (vm != null && TextBlockMouseLeftButtonDownCommand != null 
      && TextBlockMouseLeftButtonDownCommand.CanExecute(null)) 
     { 
      vm.TextBlockMouseLeftButtonDownCommand.Execute(null) 
     } 
    } 
} 
+0

घटना को आदेश निष्पादन डालने का अच्छा विचार है, लेकिन मैं कल अटैच कमांडर को आजमाउंगा! उत्तर के लिए बहुत बहुत धन्यवाद! –

+0

AttachCommandBehavior पूरी तरह से काम करता है। एक बात यह है कि मुझे यकीन नहीं है कि कैसे हल किया जाए। मैं न केवल एक घटना के लिए आदेशों को बांधना चाहता हूं, और मुझे इस तरह का समाधान मिला: http://stackoverflow.com/questions/926451/how-can-i-attach-two-attached-behaviors-to-one- xaml-element यह वास्तव में अच्छा है, लेकिन मैं इस कमांडबियरर कोलेक्शन को शैली में कैसे रख सकता हूं? आपके उत्तर के लिए अग्रिम धन्यवाद! –

+0

@ ज़ोलटन बरना यह स्टाइल या टेम्पलेट्स में काम नहीं करता है – Rachel

3

आप MVVM का उपयोग कर रहे हैं, मैं आप Galasoft MVVM Light ToolkitEventToCommand

+0

मैंने आयताकार और घटनाक्रमियों के साथ उदाहरण देखा, और यह घटनाक्रमियों के साथ भी काम कर रहा है? –

0

this question पर My answer किसी भी बाहरी उपकरण किट/पुस्तकालयों के बिना काम कर देता है। हालांकि, यह RelativeSource का उपयोग नहीं करता है, और यह 100% एमवीवीएम नहीं है। कोड-बैक इवेंट हैंडलर में कोड की एक पंक्ति की आवश्यकता होती है।

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