Monday, June 15, 2015

Passing EventArguments to RelayCommand in MVVM

I was to override the default behavior of the editable ComboBox control. Where if clicked the in the combobox the text will always be selected.

The View :
<ComboBox x:Name="cboList" FontSize="25"
     TabIndex="8" MaxDropDownHeight="270"
     Style="{DynamicResource ComboBoxStyleLargeScroll}"
     DisplayMemberPath="Name" SelectedValuePath="RepID"
     ItemsSource="{Binding VisitorHostList, UpdateSourceTrigger=PropertyChanged
   ,ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
     SelectedItem="{Binding UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, ValidatesOnDataErrors=True
   ,NotifyOnValidationError=True, Path=SelectedVisitorHost}"
     SelectedValue="{Binding Inductee.HostRepID, UpdateSourceTrigger=PropertyChanged
   ,ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" IsEditable="True" VerticalContentAlignment="Bottom" MinWidth="0" MaxWidth="351">
                                    <ComboBox.Resources>
                                        <Style TargetType="{x:Type ScrollBar}">
                                            <Setter Property="Width" Value="50" />
                                        </Style>
                                    </ComboBox.Resources>
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="GotFocus">
                                            <cmd:EventToCommand Command="{Binding GotFocusCommand, Mode=OneWay}" MustToggleIsEnabledValue="True"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="PreviewMouseUp">
                                            <cmd:EventToCommand Command="{Binding PreviewMouseUpCommand, Mode=OneWay}" MustToggleIsEnabledValue="True" PassEventArgsToCommand="True" />
                                        </i:EventTrigger>

                                    </i:Interaction.Triggers>
                                </ComboBox>
The ViewModel :
#region Commands
  public ICommand PreviewMouseUpCommand { get; set; }
 #endregion
#region Constructor
        public MyViewModel()
        {
         
            PreviewMouseUpCommand = new RelayCommand<MouseButtonEventArgs>(PreviewMouseUp);
         
        }
#endregion
#region eventhandler
private void PreviewMouseUp(MouseButtonEventArgs e)
        {
            try
            {
                System.Windows.Controls.ComboBox cboHostList = e.Source as System.Windows.Controls.ComboBox;
                if (cboHostList != null)
                {
                    var cboEditableTextBox = (cboHostList.Template.FindName("PART_EditableTextBox", cboHostList) as System.Windows.Controls.TextBox);
                    if (cboEditableTextBox != null && !cboHostList.IsDropDownOpen)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            cboEditableTextBox.SelectAll();
                            cboEditableTextBox.Focus();
                        }));
                    }
                }
            }
            catch(Exception ex)
            {
                Util.Logger(typeof(MyViewModel)).Error("Error in PreviewMouseUp of ComboboxList", ex);
            }
        }
#endregion

No comments:

Post a Comment