0
<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>

在鼠标左键按下时,它变为可编辑

4

1 回答 1

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;
        }
于 2012-10-02T18:04:44.140 回答