Frage

This is my scenario: My DataTemplate of a ListView contains a TextBox and some buttons, one of the buttons is used to select and highlight all of the text in the TextBox. I can find many solutions for select and highlight text in TextBox from code behind, but none of them define the TextBox and the Button in DataTemplate. Anyone can help?

Thanks

War es hilfreich?

Lösung

You can do something like this below :

XAML :

<Window x:Class="SOWPF.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">
<Grid>
    <ListView Width="200" Height="300" ItemsSource="{Binding FriendList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBox Width="100" Margin="2" Text="{Binding Name}"></TextBox>
                    <Button Content="Select" Click="Button_Click"></Button>
                    <Button Content="Delete"></Button>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

Code Behind :

using System.Windows;
using System.Windows.Controls;
namespace SOWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var friendViewModel = new FriendViewModel();
            friendViewModel.AddFriends();
            this.DataContext = friendViewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var parent = (StackPanel)((sender as Button).Parent);
            var children = parent.Children;
            foreach(var child in children)
            {
                if (child.GetType().Equals(typeof(TextBox)))
                {
                    var tb = child as TextBox;
                    tb.Focus();
                    tb.SelectAll();
                    break;
                }
            }
        }
    }
}

ViewModel :

using System.Collections.ObjectModel;
namespace SOWPF
{
    public class FriendViewModel
    {
        public ObservableCollection<Friend> FriendList 
        { get; set; }

        public void AddFriends()
        {
            FriendList = new ObservableCollection<Friend>();
            FriendList.Add(new Friend() { Name = "Arpan" });
            FriendList.Add(new Friend() { Name = "Nrup" });
            FriendList.Add(new Friend() { Name = "Deba" });
        }

    }

    public class Friend
    {
        public string Name { get; set; }
    }
}

Andere Tipps

Probably would be a good using an Attached property set on the button, and in the attached code using a code something like written by cvraman. Using this way you absolutely avoid the code behind structure, and better way to using mvvm

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top