如何关注鼠标左键上的wpf中的组合框?

时间:2012-10-02 08:48:57

标签: wpf

<ComboBox   TextSearch.TextPath="MemberFullName"  IsEditable="True" Height="23" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Margin="5,0,0,0" ItemsSource="{Binding MemberCollection}" SelectedItem="{Binding SelectedSearchMember,Mode=TwoWay,ValidatesOnDataErrors=True}">
            <ComboBox.ItemTemplate>
              <DataTemplate>
                   <TextBlock Text="{Binding MemberFullName}" VerticalAlignment="Center"/>
               </DataTemplate>
            </ComboBox.ItemTemplate>
</ComboBox>

鼠标左键向下按钮变为可编辑

1 个答案:

答案 0 :(得分:0)

如果您使用MVVM模式,请添加到您的ViewModel变量:

  private bool _isEditableComboBox = false;
        public bool IsEditableComboBox
        {
            get { return _isEditableComboBox; }
            set { _isEditableComboBox = value; RaisePropertyChanged(() => IsEditableComboBox); }
        }

添加到项目大会:System.Windows.Interactivity

将View命名空间添加到此程序集:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

将您的ComboBox更改为:

 <ComboBox TextSearch.TextPath="MemberFullName" IsEditable="{Binding IsEditableComboBox, UpdateSourceTrigger=PropertyChanged}" Height="23" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Margin="5,0,0,0" ItemsSource="{Binding MemberCollection}" SelectedItem="{Binding SelectedSearchMember,Mode=TwoWay,ValidatesOnDataErrors=True}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseDown">
                    <i:InvokeCommandAction Command="{Binding TurnOnEditMode}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding MemberFullName}" VerticalAlignment="Center"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

您必须在ViewModel中添加以下命令:

public ICommand TurnOnEditMode { get; private set; }
        private void OnTurnoOnEditMode()
        {
            IsEditableComboBox = true;
        }