2009-05-22 12 views
19

में मेनूइटम के लिए कमांड निर्दिष्ट करें मेरे पास एक संदर्भ मेनू है। यह कुछ संग्रह के लिए बाध्य कर रहा है और वह इस तरह का परिभाषित ItemTemplate है:DataTemplate

<ContextMenu 
    ItemsSource={Binding ...} 
    ItemTemplate={StaticResource itemTemplate} 
    /> 

ItemTemplate एक TextBlock के साथ एक सरल DataTemplate है:

<DataTemplate x:Key="itemTemplate"> 
    <TextBlock Text={Binding ...} /> 
</DataTemplate> 

मैं कैसे अंतर्निहित वस्तु की संपत्ति के MenuItem के लिए कमान संपत्ति के लिए बाध्य करना ?

उत्तर

24

मुझे लगता है कि आप एक MenuItem में अपने TextBlock रैप करने के लिए की जरूरत है:

<DataTemplate x:Key="itemTemplate"> 
    <MenuItem Command={Binding ...}> 
     <TextBlock Text={Binding ...} /> 
    </MenuItem> 
</DataTemplate> 

लेकिन मैं यह कोशिश करने के लिए मेरे सामने एक IDE की जरूरत नहीं है अभी। मुझे बताएं कि यह कैसे जाता है।


की तरह आप के रूप में here देखा ItemContainerStyle उपयोग करने की आवश्यकता लग रहा है। तुम वहाँ शुरू में गलत पथ नीचे अग्रणी के लिए खेद है - लेकिन मैं एक IDE के सामने मिला है और इस काम करता है:

<ContextMenu.ItemContainerStyle> 
    <Style TargetType="MenuItem"> 
     <Setter Property="Command" Value="{Binding ...}"/> 
    </Style> 
</ContextMenu.ItemContainerStyle> 
+3

वास्तव में, यह MenuItem के मद संग्रह के लिए TextBlock जोड़ देगा। और यह मेनूइटम को किसी अन्य मेनूइटम के अंदर भी रखता है। – arconaut

+0

लेकिन आपकी पोस्ट के साथ मैंने मेनूइटम को खुद को टेम्पलेट करने की सोच शुरू कर दी, यह मेरी ज़रूरत हो सकती है। – arconaut

+0

हां, यही एक मिनट पहले मैंने खुद कोशिश की है। – arconaut

4

हालांकि यह केवल मार्टिन हैरिस के जवाब पर थोड़ा अंतर है, मैंने सोचा कि मैं साझा करते हैं वैसे भी।

<MenuItem.ItemContainerStyle> 
    <Style TargetType="MenuItem"> 
     <Setter Property="Command" Value="{x:Static v:ViewModel.CommandForAll}"/> 
     <Setter Property="CommandParameter" Value="{Binding ValueForCommand}"/> 
    </Style> 
</MenuItem.ItemContainerStyle> 

तो फिर तुम तय कर सकते हैं क्या आदेश के लिए हैंडलर में करने के लिए:

private void CommandForAll_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
    var cmdParam = e.Paramater as ExpectedType 
    if (cmdParam != null) 
     //DoStuff... 
} 
+0

यह मेरे लिए ठीक काम किया। धन्यवाद –

-2

MainWindow मैं इसे और अधिक उपयोगी पूरे संग्रह के लिए एक एकल आदेश निर्दिष्ट और यह भी एक CommandParameter साथ भेजा पाया .xaml

<Window.ContextMenu> 
     <ContextMenu x:Name="winCM"> 

      <!--<ContextMenu.ItemContainerStyle> 
       <Style TargetType="MenuItem"> 
        <Setter Property="Command" Value="{Binding CloseCommand}" /> 
       </Style> 
      </ContextMenu.ItemContainerStyle>--> 

      <MenuItem Name="menuItem_Close" Header="Close" Command="{Binding Path=DataContext.CloseCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/> 

      <!--Command="{Binding Path=PlacementTarget.DataContext.CloseCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"--> 
     </ContextMenu> 
    </Window.ContextMenu> 

MainWindow.cs

public partial class MainWindow : Window 
{ 
     private bool _canCloseCommandExecute; 

     public bool CanCloseCommand 

     { 
      get 
      { 
       return _canCloseCommandExecute; 
      } 
     } 
     private RelayCommand _closeCommand; 
     public ICommand CloseCommand 
     { 
      get 
      { 
       if (_closeCommand == null) 
       { 
        _closeCommand = new RelayCommand(p => this.OnClose(p), p => CanCloseCommand); 
       } 
       return _closeCommand; 
      } 
     } 

     public void OnClose(object obj) 
     { 
      Close(); 
     } 

     public MainWindow() 
     { 
      InitializeComponent(); 

      /* This will do the trick here */ 
      this.winCM.DataContext = this; 
      this._canCloseCommandExecute = true; 
     } 
} 

RelayCommand.cs

public class RelayCommand : ICommand 
    { 
     #region Fields 

     readonly Action<object> _execute; 
     readonly Predicate<object> _canExecute; 

     #endregion // Fields 

     #region Constructors 

     public RelayCommand(Action<object> execute) 
      : this(execute, null) 
     { 
     } 

     public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
     { 
      if (execute == null) 
       throw new ArgumentNullException("execute"); 

      _execute = execute; 
      _canExecute = canExecute; 
     } 
     #endregion // Constructors 

     #region ICommand Members 

     [DebuggerStepThrough] 
     public bool CanExecute(object parameter) 
     { 
      return _canExecute == null ? true : _canExecute(parameter); 
     } 

     public event EventHandler CanExecuteChanged 
     { 
      add 
      { 
       CommandManager.RequerySuggested += value; 
      } 
      remove 
      { 
       CommandManager.RequerySuggested -= value; 
      } 
     } 

     public void Execute(object parameter) 
     { 
      _execute(parameter); 
     } 

     #endregion // ICommand Members 
    } 
+0

कृपया ढेर पर "संरक्षित" उत्तरों पोस्ट न करें। इसका उद्देश्य ज्ञान साझा करना है, इसे अवरुद्ध नहीं करना है। – davids

+0

आपने एक प्रश्न का उत्तर दिया जो उसने नहीं पूछा, गुरु। –

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