C# ItemsControl的ListVIew:从ItemsControl中选择其项后,获取ListViewItem索引
我有一个列表视图。此ListView中有ItemsControl,其中有第二个ItemsControl。在第二个ItemsControl中有文本框 列表视图->项目控制->项目控制->文本框 单击此文本框后,是否有可能获取ListViewItem的索引,该文本框属于哪个特定的文本框 比如说 我在索引0上选择一个ListViewItem,然后单击属于索引2上ListViewItem的文本框。在这种情况下,我希望将SelectedGroupIndex的值从0更改为2。 “Hello”字符串仅用于测试 多谢各位 视图模型C# ItemsControl的ListVIew:从ItemsControl中选择其项后,获取ListViewItem索引,c#,wpf,listview,C#,Wpf,Listview,我有一个列表视图。此ListView中有ItemsControl,其中有第二个ItemsControl。在第二个ItemsControl中有文本框 列表视图->项目控制->项目控制->文本框 单击此文本框后,是否有可能获取ListViewItem的索引,该文本框属于哪个特定的文本框 比如说 我在索引0上选择一个ListViewItem,然后单击属于索引2上ListViewItem的文本框。在这种情况下,我希望将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));
}
公共类MainWindowViewModel
{
公共可见集合所有文本{get;set;}
public int SelectedGroupIndex{get;set;}
public ICommand AddGroup{get;private set;}
公共ICommand AddColumn{get;private set;}
public ICommand TextBoxSelected{get;private set;}
公共主窗口视图模型()
{
this.AllTexts=新的ObservableCollection();
this.SelectedGroupIndex=-1;
this.AddGroup=新命令(this.AddGroupCommandHandler);
this.AddColumn=新命令(this.AddColumnCommandHandler);
this.TextBoxSelected=新命令(this.TextBoxSelectedCommandHandler);
}
私有void AddGroupCommandHandler()
{
var tempColumn=新的ObservableCollection(){
这个.GetListViewString(“Hello”),
这个.GetListViewString(“Hello”),
这个.GetListViewString(“Hello”),
这个.GetListViewString(“Hello”),
这个.GetListViewString(“Hello”)};
var tempGroup=新的ObservableCollection();
添加(tempColumn);
this.AllTexts.Add(新的ObservableCollection(tempGroup));
}
私有void AddColumnCommandHandler()
{
如果(this.SelectedGroupIndex>=0&&this.SelectedGroupIndex
查看:
<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>
实际上,您需要的是将
文本框的DataContext
作为参数传递给命令,因此对其使用CommandParameter
,并使用参数执行命令:
<TextBox.InputBindings>
<MouseBinding Gesture="LeftClick"
Command="{Binding TextBoxSelected, Source={StaticResource vm}}"
CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Mode=Self}}"/>
</TextBox.InputBindings>
因此,您将使用items源集合中的一个项作为命令参数,并可以找到它的索引