0

我按照此MSDN Walktrough的说明创建了一个示例项目。我稍微更改了示例并创建了两个额外的 TextBox 控件和一个按钮。

这就是它的样子: 在此处输入图像描述

我使用代码优先方法和 DbContext 的派生类作为代码生成项创建了一个简单的模型。

之后,我使用了创建的数据源: 在此处输入图像描述

“名称” - 文本框是只读的。当按下“搜索” - 按钮时,我想允许用户输入一个 ID 并搜索它。因此,如果可以找到指定的 ID,则应更新“名称”- TextBox。我不太关心 DataGrid,因为我的实际代码中不需要它们。

我不知道如何更新 CollectionViewSource 并验证输入,因为 CollectionViewSource.View.CurrentItem 属性是只读的。

编辑:这是我的(几乎完全是设计师生成的)代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" mc:Ignorable="d"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:my="clr-namespace:SchoolModel;assembly=SchoolModel" 
        Loaded="Window_Loaded">

    <Window.Resources>
        <CollectionViewSource x:Key="departmentViewSource" d:DesignSource="{d:DesignInstance my:Department, CreateList=True}" />
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid DataContext="{StaticResource departmentViewSource}" Name="grid1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Label Content="Department ID:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="departmentIDTextBox" Text="{Binding Path=DepartmentID, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        </Grid>
        <Grid Grid.Row="1" DataContext="{StaticResource departmentViewSource}" Name="grid2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Label Content="Name:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="nameTextBox" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        </Grid>
    </Grid>
</Window>


public partial class MainWindow : Window
{
    private SchoolEntities _context = new SchoolEntities(@"data source=localhost\MSSQL2008R2;database=SchoolModel;integrated security=True;MultipleActiveResultSets=True");
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        System.Windows.Data.CollectionViewSource departmentViewSource =
            ((System.Windows.Data.CollectionViewSource)(this.FindResource("departmentViewSource")));

        // Load is an extension method on IQueryable, defined in the System.Data.Entity namespace.
        // This method enumerates the results of the query, much like ToList but without creating a list. 
        // When used with Linq to Entities this method creates the entity instances and adds to the context.
        _context.Departments.Load(); // Load is defined in the System.Data.Entity namespace.      

        // After the data is loaded call the DbSet<T>.Local property to use the DbSet<T> as a binding source. 
        departmentViewSource.Source = _context.Departments.Local;

        var src = _context.Departments.Local;
        ICollectionView colelctionView = CollectionViewSource.GetDefaultView(src);
        colelctionView.Filter = new Predicate<object>(i => (i as Department).DepartmentID.ToString() == departmentIDTextBox.Text);
    }
}
4

2 回答 2

1

@冻结2046

ICollectionView公开了以下方法来设置当前项:

  • 移动当前到第一
  • 移动当前到最后
  • 移动当前到下一个
  • 将当前移动到上一个
  • MoveCurrentTo(对象值)

我通常发现最后一种方法对于我想要使用选定项目的生产代码最有用

    public Department SelectedItem
    {
        get { return _collectionView.CurrentItem as Department; }
        set { _collectionView.MoveCurrentTo(value); }
    }

它还公开了一个 CurrentChanged 事件,您可以使用该事件来设置一个处理程序,您可以在其中执行您需要的任何操作(包括验证)以对当前正在更改的项目做出反应。

// retrieve the ICollectionView associated with the ObservableCollection
_collectionView = CollectionViewSource.GetDefaultView(src);
if (_collectionView == null) throw new NullReferenceException("_collectionView");

//listen to the CurrentChanged event to be notified when the selection changes
_collectionView.CurrentChanged += OnCollectionViewCurrentChanged;

private void OnCollectionViewCurrentChanged(object sender, EventArgs e) {
    // whatever
}

可以说更多关于过滤的信息,但我想你只是试图在这里处理一些示例代码,并陷入如何设置 CurrentItem 并对其做出反应。希望这会给您一些关于如何做到这一点的想法,但如果您需要,请随时提出更多问题。

干杯,
贝里尔

于 2011-05-28T11:38:54.633 回答
0

您的 SearchTextBox 应该绑定到 ViewModel 中的 SearchText 字段,而不是 CurrentItem 的名称

您的 ViewModel 应如下所示:

ObservableCollection<Courses> { get; set; }
ObservableCollection<Departments> { get; set; }

string SearchText { get; set; }
ICommand SearchCommand { get; }
于 2011-05-26T14:25:04.337 回答