【问题标题】:Get selected item from a bound listbox从绑定的列表框中获取选定的项目
【发布时间】:2014-03-25 03:49:11
【问题描述】:

我有一个用户列表框,我想从中获取所选项目。我使用了 selecteditem 但它总是返回零。 这是我的列表框 xaml 代码:

 <ListBox  Name="_imageList" Margin="10,10,10,0"  IsSynchronizedWithCurrentItem="True" ScrollViewer.HorizontalScrollBarVisibility="Visible"  VerticalAlignment="Top" Height="250" BorderThickness="0" SelectionChanged="List_clicked">
        <!--<ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <EventSetter Event="MouseLeftButtonDown"  Handler="ListBoxItem_MouseLeftButtonDown"/>
            </Style>
        </ListBox.ItemContainerStyle>-->
        <ListBox.ItemTemplate>
            <DataTemplate DataType="Enfant">
                <Border CornerRadius="30">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                        <Button Grid.Row="0" Width="50" Height="80" Click="btn_click">
                            <Button.Template>
                                <ControlTemplate>
                                    <Image x:Name="image" Source="{Binding avatar}"/>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                        <TextBlock Grid.Row="1" x:Name="nom" Text="{Binding prenom}" VerticalAlignment="Center"/>
                </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

这是背后的代码:

 private void btn_click(object sender, RoutedEventArgs e)
    {
        if (OnKidClick != null)
        {
            kid = new Enfant();
            OnKidClick(this, new RoutedEventArgs());
            var item = _imageList.SelectedItem;
        }
    }

【问题讨论】:

  • 据我了解,@RohitVats 之前的answer 对您有所帮助。也许你应该接受它?
  • 是的,它有帮助,而且我没有使用 MVVM,我没有找到可以帮助我的结构良好的教程
  • 你正在使用Button 他建议你的东西:Use Button in place of Image and override template of Button to give it an Image look, so that you can have clickable image.,与 MVVM 无关。

标签: c# wpf listbox


【解决方案1】:

您已经在SelectionChanged="List_clicked" 方法中处理了所选项目,在该方法中执行您需要执行的操作。

【讨论】:

  • 是的,我做到了,但它总是返回列表中的第一项而不是选定的项。
【解决方案2】:

您可以将 ICollectionView 用于 ListBox 源,您可以使用 CurrentItem 属性轻松选择项目。

 public class UserInfoViewModel 
  {
    private ICollectionView _employeeCollectionView;
    public ICollectionView EmployeeCollectionView
    {
        get
        {
            return _employeeCollectionView;
        }
        private set { _employeeCollectionView = value; }
    }
  private void GetEmployee()
    {
     EmployeeCollectionView = CollectionViewSource.GetDefaultView(HERE-IS-YOUR-COLLECTION);

     EmployeeCollectionView.CurrentChanged += new EventHandler(_customerView_CurrentChanged);
   }

 void _customerView_CurrentChanged(object sender, EventArgs e)
    {
      var selectedEmployee= EmployeeCollectionView.CurrentItem as Employee;
    }
}

将 ListBox 绑定到您的集合。

<ListBox ItemsSource="{Binding EmployeeCollectionView, UpdateSourceTrigger=PropertyChanged,     NotifyOnSourceUpdated=True}"   IsSynchronizedWithCurrentItem="True">


</ListBox >

希望对您有所帮助。

【讨论】:

  • 请您进一步解释如何将我的列表框绑定到 ICollectionView
【解决方案3】:

我会做 MVVM 风格。然后在您的 ListBox 中,设置属性

SelectedItem = "{Binding SelectedEmployee}"

然后在为您的 ListBox 提供数据的任何 ViewModel 中创建一个 SelectedEmployee 属性(此 View 的 DataContext)。当所选项目发生更改时,将始终调用您的属性的设置器。

我在这里发布了一些类似的代码,演示了如何使用绑定来连接视图和视图模型:

Hiding A Listbox Item in WPF

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2011-08-23
    相关资源
    最近更新 更多