如何在列表框中加载所选项目?

本文关键字:选项 项目 加载 列表 | 更新日期: 2023-09-27 18:07:03

我正在做一个班级项目,我在设置列表框中的选定项时遇到了困难。我创建了一个狗类,其中包含一个可观察的品种集合。在我的主屏幕上有一个列表框,你可以从中选择一只狗。当选择一条狗并触发更新狗按钮事件时,将打开更新狗屏幕。在更新狗的屏幕有一个列表框与所有的狗品种。我的问题是,我如何设置列表框中的选定项目以链接到狗的品种?

下面是btnUpdateDog_Click事件的代码:
    private void btnUpdateDog_Click(object sender, RoutedEventArgs e)
    {
        if (this.lstDogListBox.SelectedItem != null)
        {
            Dog selectedDog = (Dog)this.lstDogListBox.SelectedItem;
            var dogId = selectedDog.DogId;
            var _selectedDog = _dogRepository.Fetch(dogId).SingleOrDefault();
            UpdateDogWindow updateDogWindow = new UpdateDogWindow(dogId);
            updateDogWindow.Owner = windowClientApplication;
            updateDogWindow.DataContext = _selectedDog;
            updateDogWindow.ShowDialog();
            if (updateDogWindow.ShowDialog() == true)
            {
                LoadSearchResults();
            }
        }
    }

下面是UpdateDogWindow

的构造函数
    public UpdateDogWindow(int criteria)
    {
        _dogRepository = new DogRespository();
        var _dogQuery = _dogRepository.Fetch(criteria);
        this.DataContext = _dogQuery;
        InitializeComponent();
    }
下面是UpdateDogWindow的XAML代码:
<Window x:Class="ClientApplication.UpdateDogWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Objects="clr-namespace:SGVHS_VCDB.Database.Objects;assembly=SGVHS_VCDB.Database.Objects"
        Title="Update Dog"
        Height="300" 
        Width="800" 
        Background="#FFD6DBDD" 
        WindowStartupLocation="CenterOwner" 
        WindowStyle="ToolWindow" 
        Loaded="Window_Loaded">
    <Window.DataContext>
        <Objects:Dog />
    </Window.DataContext>
    <Canvas>
        <Label 
            Canvas.Left="20" 
            Canvas.Top="20" 
            Content="_Dog Name:" 
            Height="36" 
            FontSize="18" 
            Name="lblDogName" />
        <TextBox 
            Canvas.Left="130" 
            Canvas.Top="20" 
            Height="38" 
            Width="250" 
            FontSize="18" 
            Text="{Binding Path=DogName, UpdateSourceTrigger=PropertyChanged}" 
            Name="txtUpdateDogName" />
        <Label 
            Canvas.Left="20" 
            Canvas.Top="80" 
            Content="_Intake Date:" 
            Height="36" 
            FontSize="18" 
            Name="lblIntakeDate" />
        <DatePicker 
            Canvas.Left="170" 
            Canvas.Top="81" 
            Height="38" 
            Width="175" 
            FontSize="18" 
            SelectedDate="{Binding IntakeDate}"
            Name="dtoUpdateIntakeDate"/>
        <Label
            Canvas.Left="20" 
            Canvas.Top="140"
            Height="36"
            Content="_Unavailable Date:"
            FontSize="18"
            Name="lblUnavailableDate"/>
        <DatePicker 
            Canvas.Left="170" 
            Canvas.Top="141" 
            Height="38" 
            Width="175" 
            FontSize="18" 
            SelectedDate="{Binding UnavailableDate}" 
            Name="dtoUnavailableDate"/>
        <GroupBox 
            Canvas.Left="400" 
            Canvas.Top="22" 
            Header="Breeds" 
            Height="169" 
            Width="363" 
            FontSize="18" 
            BorderBrush="Black"
            Name="grpBreeds">
            <ListBox 
                DisplayMemberPath="BreedName"
                SelectionMode="Multiple"
                SelectedValuePath="BreedName"
                SelectedItem="{Binding Breed, Mode=TwoWay}"
                Name="lstUpdateDogBreedList" />
        </GroupBox>
        <CheckBox 
            Canvas.Left="20" 
            Canvas.Top="200" 
            Content="Adopted" 
            Height="32" 
            FontSize="18"
            IsChecked="{Binding Adopted}"
            Name="chkAdopted"/>
        <Button 
            Canvas.Left="637" 
            Canvas.Top="205" 
            Content="Done" 
            Height="30" 
            Width="120" 
            FontSize="18" 
            Name="btnUpdateDogDone" />        
    </Canvas>
</Window>

我试图将品种列表的绑定设置为双向,为什么不工作?

如何在列表框中加载所选项目?

看起来像作业。答案是使用CollectionViewSource进行过滤。你可以使用CollectionViewSource的过滤,排序等

http://weblogs.asp.net/psheriff/archive/2010/08/03/filtering collectionview -数据-在- silverlight 4. - aspx