Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从绑定的列表框中获取所选项目_C#_Wpf_Listbox - Fatal编程技术网

C# 从绑定的列表框中获取所选项目

C# 从绑定的列表框中获取所选项目,c#,wpf,listbox,C#,Wpf,Listbox,我有一个用户列表框,我想从中获取所选项目。我使用了selecteditem,但它总是返回零。 这是我的listbox xaml代码: <ListBox Name="_imageList" Margin="10,10,10,0" IsSynchronizedWithCurrentItem="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" VerticalAlignment="Top" Height="250" Bo

我有一个用户列表框,我想从中获取所选项目。我使用了selecteditem,但它总是返回零。 这是我的listbox 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>

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

您可以使用ICollectionView for ListBox source,并且可以使用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 ItemsSource="{Binding EmployeeCollectionView, UpdateSourceTrigger=PropertyChanged,     NotifyOnSourceUpdated=True}"   IsSynchronizedWithCurrentItem="True">


</ListBox >


希望对您有所帮助。

我会用MVVM的方式来做。然后在列表框中设置属性

SelectedItem = "{Binding SelectedEmployee}"
然后在为ListBox(此视图的DataContext)提供数据的ViewModel中创建SelectedEmployee属性。当所选项目发生更改时,将始终调用属性的setter

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


是的,我做了,但它总是返回列表中的第一项,而不是所选项。据我所知,@RohitVats的前一项对您有所帮助。也许你应该接受它?是的,它有帮助,而且我没有使用MVVM,我没有找到一个结构良好的教程可以帮助我。你正在使用
按钮
他建议你的:
使用按钮代替图像,并覆盖按钮的模板以给它一个图像外观,这样你就可以有可点击的图像。
,MVVM与此无关。请您进一步解释如何将我的列表框绑定到ICollectionView
SelectedItem = "{Binding SelectedEmployee}"