0

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

这是 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>

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

4

1 回答 1

0

它看起来像一个家庭作业。答案是使用 CollectionViewSource 进行过滤。您可以使用 CollectionViewSource 进行过滤、排序等。

http://weblogs.asp.net/psheriff/archive/2010/08/03/filtering-collectionview-data-in-silverlight-4.aspx

于 2013-04-01T09:01:58.007 回答