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

时间:2013-04-01 05:33:19

标签: c# xaml data-binding

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

以下是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>

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

1 个答案:

答案 0 :(得分:0)

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

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