【问题标题】:ListVIew of ItemsControl: Get the ListViewItem index after selecting one its item from ItemsControlItemsControl 的 ListVIew:从 ItemsControl 中选择一项后获取 ListViewItem 索引
【发布时间】:2018-08-08 04:59:51
【问题描述】:

我有一个列表视图。在这个 ListView 里面有 ItemsControl,里面有第二个 ItemsControl。在第二个 ItemsControl 里面有 TextBoxes。

ListView -> ItemsControl -> ItemsControl -> TextBox

点击这个TextBox后,有没有可能获得ListViewItem的索引,具体的TextBox属于哪个?

例如

我在索引 0 上选择了一个 ListViewItem,然后我点击了属于索引 2 上 ListViewItem 的 TextBox。在这种情况下,我想将 SelectedGroupIndex 的值从 0 更改为 2。 "Hello" 字符串仅用于测试。

非常感谢。

视图模型

public class MainWindowViewModel
    {
        public ObservableCollection<ObservableCollection<ObservableCollection<ListViewString>>> AllTexts { get; set; }

        public int SelectedGroupIndex { get; set; }

        public ICommand AddGroup { get; private set; }

        public ICommand AddColumn { get; private set; }

        public ICommand TextBoxSelected { get; private set; }

        public MainWindowViewModel()
        {
            this.AllTexts = new ObservableCollection<ObservableCollection<ObservableCollection<ListViewString>>>();
            this.SelectedGroupIndex = -1;
            this.AddGroup = new Command(this.AddGroupCommandHandler);
            this.AddColumn = new Command(this.AddColumnCommandHandler);
            this.TextBoxSelected = new Command(this.TextBoxSelectedCommandHandler);
        }

        private void AddGroupCommandHandler()
        {
            var tempColumn = new ObservableCollection<ListViewString>() {
                this.GetListViewString("Hello"),
                this.GetListViewString("Hello"),
                this.GetListViewString("Hello"),
                this.GetListViewString("Hello"),
                this.GetListViewString("Hello") };
            var tempGroup = new ObservableCollection<ObservableCollection<ListViewString>>();
            tempGroup.Add(tempColumn);
            this.AllTexts.Add(new ObservableCollection<ObservableCollection<ListViewString>>(tempGroup));
        }

        private void AddColumnCommandHandler()
        {
           if (this.SelectedGroupIndex >= 0 && this.SelectedGroupIndex < this.AllTexts.Count)
           {
                var tempColumn = new ObservableCollection<ListViewString>() {
                    this.GetListViewString("Hello"),
                    this.GetListViewString("Hello"),
                    this.GetListViewString("Hello"),
                    this.GetListViewString("Hello"),
                    this.GetListViewString("Hello") };
                this.AllTexts[this.SelectedGroupIndex].Add(tempColumn);
           }
        }

        private void TextBoxSelectedCommandHandler()
        {
            // TODO: Change SelectedItem of ListView  

            // this.SelectedGroupIndex = ...;
        }

        private ListViewString GetListViewString(string text)
        {
            return new ListViewString { Value = text };
        }

        private string GetTextFromListViewString(ListViewString listViewString)
        {
            return listViewString.Value;
        }
    }

    /// <summary>
    /// Class used to show user Text in ListView.
    /// Using this class fixes the issue that ObservableCollection didn't update
    /// after user changed values of TextBoxes in GUI.
    /// </summary>
    public class ListViewString : DependencyObject
    {
        public string Value
        {
            get
            {
                return (string)GetValue(ValueProperty);
            }

            set
            {
                SetValue(ValueProperty, value);
            }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(ListViewString), new PropertyMetadata(string.Empty));
    }

查看:

<Window.Resources>
    <ResourceDictionary>
        <local:MainWindowViewModel x:Key="vm" />
    </ResourceDictionary>
</Window.Resources>

<Grid Margin="10,10,10,10" VerticalAlignment="Top">
    <Grid.RowDefinitions>
        <RowDefinition Height="300" />
        <RowDefinition />
    </Grid.RowDefinitions>

    <ListView Grid.Row="0"
    ItemsSource="{Binding AllTexts, Source={StaticResource vm}, Mode=TwoWay}"
    Background="Blue"
    SelectedIndex="{Binding SelectedGroupIndex, Source={StaticResource vm}}">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Vertical" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBox Text="{Binding Value}"
                                                VerticalContentAlignment="Center"
                                                HorizontalContentAlignment="Center"
                                                Width="100" Height="40">
                                            <TextBox.InputBindings>
                                                <MouseBinding Gesture="LeftClick"
                                                Command="{Binding TextBoxSelected, Source={StaticResource vm}}" />
                                            </TextBox.InputBindings>
                                        </TextBox>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,20,0,0">
        <Button Content="Add Group" Width="120" Height="30"
        Command="{Binding AddGroup, Source={StaticResource vm}}" />
        <Button Content="Add Column" Margin="20,0,0,0" Width="120" Height="30"
        Command="{Binding AddColumn, Source={StaticResource vm}}" />
        <TextBlock Width="120" Height="30" FontSize="20" Margin="20,0,0,0"
        Text="{Binding SelectedGroupIndex, Source={StaticResource vm}}" />
    </StackPanel>
</Grid>

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

    实际上,您需要将TextBoxDataContext 作为参数传递给命令,因此请使用CommandParameter 并使用参数实现您的命令:

    <TextBox.InputBindings>
        <MouseBinding Gesture="LeftClick" 
                      Command="{Binding TextBoxSelected, Source={StaticResource vm}}" 
                      CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Mode=Self}}"/>
    </TextBox.InputBindings>
    

    因此,您将从项目源集合中获得一个项目作为命令参数,并且可以找到它的索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      相关资源
      最近更新 更多