我在网格视图单元格中嵌入了一个组合框,随后嵌入到ListView对象中。加载窗口时,组合框从我的容器类列表中获取值并填充组合框。这很好,但是什么 我希望能够在每次触发SelectionChanged事件时确定组合框的列表视图索引。
<ComboBox x:Name="TransFileOpt" ItemsSource="{Binding ComboBoxOptions}" SelectedIndex="{Binding SelectedIndex}" SelectionChanged="ComboBoxSelectionChanged"/>
当我之前搜索过此内容时,所有结果都指向System.Windows.Forms.ListView,而我正在使用System.Windows.Controls.ListView,所以
m_ListView.Rows[index]
不起作用。
基本上我需要一种方法来获取包含调用ComboBoxSelectionChanged的组合框的ListView索引或项目(触发SelectionChanged事件时)。
ListView块:
<ListView Name="m_ListView" ItemsSource="{Binding Tables}" Margin="0,28,0,132" SelectionMode="Extended" SelectedIndex="0"
RenderTransformOrigin="0.503,0.468" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView x:Name="m_TableGridView" AllowsColumnReorder="False" >
<GridViewColumn Width="50" Header="Loaded">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Loaded}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150" Header="Master File" DisplayMemberBinding ="{Binding MasterFileName}"/>
<GridViewColumn Width="200" Header ="Translation File" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="TransFileOpt" ItemsSource="{Binding ComboBoxOptions}"
SelectedIndex="{Binding SelectedIndex}" SelectionChanged="ComboBoxSelectionChanged"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text }"></Setter>
</Style>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.Resources>
</ListView>
其他信息:该项目是通过Visual Studio的wpf编辑器生成的,该编辑器默认为类的System.Windows.Controls。
答案 0 :(得分:1)
以下是您的工作示例:
<Window x:Class="ListViewGridView.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 Margin="10" Name="lvUsers" ItemsSource="{Binding items}">
<ListView.View>
<GridView>
<GridViewColumn Header="Items" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding containerItems}" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
代码隐藏:
public partial class MainWindow : Window
{
public ObservableCollection<ItemContainer> items { get; set; }
public MainWindow()
{
InitializeComponent();
items = new ObservableCollection<ItemContainer>();
items.Add(new ItemContainer() { containerItems = new List<string>() { "Item1", "Item2", "Item3" } });
items.Add(new ItemContainer() { containerItems = new List<string>() { "Item4", "Item5", "Item6" } });
this.DataContext = this;
}
public static T FindAncestorOrSelf<T>(DependencyObject obj)
where T : DependencyObject
{
while (obj != null)
{
T objTest = obj as T;
if (objTest != null)
return objTest;
obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListViewItem lvItem = FindAncestorOrSelf<ListViewItem>(sender as ComboBox);
ListView listView = ItemsControl.ItemsControlFromItemContainer(lvItem) as ListView;
int index = listView.ItemContainerGenerator.IndexFromContainer(lvItem);
Console.WriteLine(index.ToString());
}
}
简单的模型类:
public class ItemContainer
{
public List<string> containerItems { get; set; }
}
就是这样。玩得开心!