如何在焦点位于文本框时选择整个文本,文本框中的位置在wpf的GridView列中

时间:2014-12-05 10:11:06

标签: wpf

默认情况下我有一个ListView,但是当点击一个按钮时我会将视图更改为GridView(我还没有包含按钮,这是示例代码)。在GridView中,对于创建datatemplate的列,其中文本框可用。现在当焦点在文本框上时,我希望选择整个文本。但事情并没有发生。

 <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">
    <Window.Resources>

        <Style TargetType="{x:Type TextBox}" >
            <Setter Property="BorderThickness" Value="0"/>
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="IsReadOnly" Value="False" />
                </Trigger>
                <Trigger Property="IsFocused" Value="False">
                    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=Background}"/>
                    <Setter Property="IsReadOnly" Value="True" />
                </Trigger>
                <Trigger Property="IsReadOnly" Value="False">
                    <Setter Property="Background" Value="White"/>                    
                </Trigger>
            </Style.Triggers>
        </Style>

        <DataTemplate x:Key="DefaultTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="EditableTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>

        <Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
            <Setter Property="View">
                <Setter.Value>
                    <GridView >
                        <GridViewColumn Header="Name" CellTemplate="{StaticResource DefaultTemplate}"/>
                        <GridViewColumn Header="Age">
                            <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBox Text="{Binding Age}" GotFocus="TextBox_GotFocus"/>
                                </StackPanel>
                            </DataTemplate>
                            </GridViewColumn.CellTemplate>

                        </GridViewColumn>

                    </GridView>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <StackPanel>       
        <ListView x:Name="FoldersListView" Margin="3" SelectionMode="Single"  ItemsSource="{Binding Path=ObjectCollection}"                  
                         IsSynchronizedWithCurrentItem="True" Style="{StaticResource ListViewStyle}">

        </ListView>
     </StackPanel>

</Window>

以下是代码:

   using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{

    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public testVM VM = new testVM();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = VM;
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {           
                (sender as TextBox).SelectAll();  

        }     
    }

    public class testVM : ViewModelBase
    {
        List<emplyee> myProperty = new List<emplyee>();

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        public testVM()
        {
            objectCollection.Add(new emplyee() { Name = "aaa India",Age="10" });
            objectCollection.Add(new emplyee() { Name = "bbb India",Age="20" });
            objectCollection.Add(new emplyee() { Name = "ccc India",Age="30" });
            objectCollection.Add(new emplyee() { Name = "ddd India",Age="40" });
        }

        public List<emplyee> MyProperty { get{return myProperty;} }

        private ObservableCollection<emplyee> objectCollection = new ObservableCollection<emplyee>();
        public ObservableCollection<emplyee> ObjectCollection
        {
            get
            {


                return objectCollection;
            }
            set
            {
                objectCollection = value;

            }
        }


    }

    public class emplyee
    {
        public emplyee()
        {

        }

        public string Name { get; set; }
        public string Age { get; set; }

    }
}
我错过了什么?请建议思考......我已经尝试了所有不同的方式。

1 个答案:

答案 0 :(得分:0)

在您的XAML中,将其添加到样式

<Style TargetType="{x:Type TextBox}">
    <EventSetter Event="UIElement.GotKeyboardFocus" Handler="TextBox_GotKeyboardFocus" />
</Style>

在您的代码中,

private void TextBox_GotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
{
    ((TextBox)sender).SelectAll();
}