无法读取wpf中的选定项目数据
cant read selected item data in wpf
您好,我已将一些数据加载到我的数据模板中,如您在图片中所见,我有以下数据:
项目编号是:0
ItemNumber 是:1
ItemNumber 是:2
ItemNumber 是:3
...
first image
但是当我想要读取数据时,我只得到
项目编号是:0
second image
问题出在哪里?
private void lvDataBinding_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem myListBoxItem = (ListBoxItem)(lvDataBinding.ItemContainerGenerator.ContainerFromItem(lvDataBinding.Items.CurrentItem));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("txtTitle", myContentPresenter);
Console.WriteLine("The text of the TextBlock of the selected list item: "
+ myTextBlock.Text);
}
我可以通过以下代码找到 selectedItem 索引:
private void ListBoxItem_MouseEnter(object sender, MouseEventArgs e)
{
ListBoxItem lbi = sender as ListBoxItem;
lbi.IsSelected = true;
selectedItemIndex = lvDataBinding.SelectedIndex;
}
private void ListBoxItem_MouseLeave(object sender, MouseEventArgs e)
{
lvDataBinding.SelectedItems.Clear();
}
更新:
<ListView Name="lvDataBinding" Grid.Row="1" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="0,0,0,175" Background="{x:Null}" SelectionChanged="lvDataBinding_SelectionChanged">
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="MouseEnter" Handler="ListBoxItem_MouseEnter"/>
<EventSetter Event="MouseLeave" Handler="ListBoxItem_MouseLeave"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Focusable" Value="False" />
<Setter Property="TabIndex" Value="-1"/>
<Setter Property="IsTabStop" Value="True"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Metro:MetroCanvasGrid Background="#f5f6fa" TabIndex="-1" Focusable="False">
<StackPanel Margin="1,1,1,1" VerticalAlignment="Top">
<Grid VerticalAlignment="Top" Background="#ffffff" Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Margin="5" Orientation="Horizontal">
<Image Height="20" Width="20" Source="..\Resources\Delete.png" Cursor="Hand">
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.DeleteCommand}" />
</Image.InputBindings>
</Image>
<Image Height="20" Width="20" Source="..\Resources\Edit.png" Cursor="Hand">
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.EditCommand}"/>
</Image.InputBindings>
</Image>
</StackPanel>
<TextBlock Name="txtTitle" Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="{Binding Path=Title}"
Margin="0,5,5,27"/>
<TextBlock Name="txtContent" Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="{Binding Path=Content}"
Margin="0,27,5,5"/>
</Grid>
</StackPanel>
</Metro:MetroCanvasGrid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是访问 DataTemplate
中元素的更好方法
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
yield return (T)child;
foreach (T childOfChild in FindVisualChildren<T>(child))
yield return childOfChild;
}
}
}
现在假设您要从数据模板访问文本块:
foreach (var txtblk in FindVisualChildren<TextBlock>(this))
{
if (txtblk.Name == "txtTitle")
{
////do what u want
}
}
更新
要仅获取所选项目,请使用:
var selectedItems = MyListView.SelectedItems;
foreach (ListViewItem selectedItem in selectedItems)
{
//codes here
}
您好,我已将一些数据加载到我的数据模板中,如您在图片中所见,我有以下数据:
项目编号是:0
ItemNumber 是:1
ItemNumber 是:2
ItemNumber 是:3
...
first image
但是当我想要读取数据时,我只得到
项目编号是:0
second image
问题出在哪里?
private void lvDataBinding_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem myListBoxItem = (ListBoxItem)(lvDataBinding.ItemContainerGenerator.ContainerFromItem(lvDataBinding.Items.CurrentItem));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("txtTitle", myContentPresenter);
Console.WriteLine("The text of the TextBlock of the selected list item: "
+ myTextBlock.Text);
}
我可以通过以下代码找到 selectedItem 索引:
private void ListBoxItem_MouseEnter(object sender, MouseEventArgs e)
{
ListBoxItem lbi = sender as ListBoxItem;
lbi.IsSelected = true;
selectedItemIndex = lvDataBinding.SelectedIndex;
}
private void ListBoxItem_MouseLeave(object sender, MouseEventArgs e)
{
lvDataBinding.SelectedItems.Clear();
}
更新:
<ListView Name="lvDataBinding" Grid.Row="1" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="0,0,0,175" Background="{x:Null}" SelectionChanged="lvDataBinding_SelectionChanged">
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="MouseEnter" Handler="ListBoxItem_MouseEnter"/>
<EventSetter Event="MouseLeave" Handler="ListBoxItem_MouseLeave"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Focusable" Value="False" />
<Setter Property="TabIndex" Value="-1"/>
<Setter Property="IsTabStop" Value="True"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Metro:MetroCanvasGrid Background="#f5f6fa" TabIndex="-1" Focusable="False">
<StackPanel Margin="1,1,1,1" VerticalAlignment="Top">
<Grid VerticalAlignment="Top" Background="#ffffff" Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Margin="5" Orientation="Horizontal">
<Image Height="20" Width="20" Source="..\Resources\Delete.png" Cursor="Hand">
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.DeleteCommand}" />
</Image.InputBindings>
</Image>
<Image Height="20" Width="20" Source="..\Resources\Edit.png" Cursor="Hand">
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.EditCommand}"/>
</Image.InputBindings>
</Image>
</StackPanel>
<TextBlock Name="txtTitle" Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="{Binding Path=Title}"
Margin="0,5,5,27"/>
<TextBlock Name="txtContent" Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="{Binding Path=Content}"
Margin="0,27,5,5"/>
</Grid>
</StackPanel>
</Metro:MetroCanvasGrid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是访问 DataTemplate
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
yield return (T)child;
foreach (T childOfChild in FindVisualChildren<T>(child))
yield return childOfChild;
}
}
}
现在假设您要从数据模板访问文本块:
foreach (var txtblk in FindVisualChildren<TextBlock>(this))
{
if (txtblk.Name == "txtTitle")
{
////do what u want
}
}
更新
要仅获取所选项目,请使用:
var selectedItems = MyListView.SelectedItems;
foreach (ListViewItem selectedItem in selectedItems)
{
//codes here
}