【问题标题】:cant read selected item data in wpf无法在 wpf 中读取所选项目数据
【发布时间】:2018-04-01 11:52:29
【问题描述】:

您好,我已将一些数据加载到我的数据模板中,正如您在图片中看到的那样,我有以下数据:
项目编号为:0
商品编号为:1
商品编号为:2
商品编号为: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>

【问题讨论】:

  • 你到底想做什么?
  • 如果它的列表框前几行你的代码没有意义或至少是无用/额外的代码(如果你试图获取所选项目)
  • 你为什么不添加你的 xaml ?
  • 和你的数据模板中的文本块,它们有绑定吗?
  • @Kajbo 我想读取所选项目数据

标签: c# wpf datatemplate listboxitem


【解决方案1】:

这是访问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;
   }
 }
}

现在假设你想从 dataTemplate 访问一个文本块:

  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
}  

【讨论】:

  • tnx 但现在我有另一个问题,我想在单击我的按钮时获取所选项目的值,您的代码会获取所有项目的值。
  • 我不关注?我的代码只会访问名为 txtTitle 的文本块,这不是你想要的吗?
  • 如果你看到我的第一张图片,你可以看到我有一些项目,每个项目都有 2 个按钮(编辑/删除),所以我希望当用户点击编辑时我得到当前项目值而不是所有项目
  • current item values 这是什么意思??可以举个例子吗?
  • 问题在于当前项目没有改变
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-29
  • 2017-03-29
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多