使用WPF进行数据绑定 - 在输入上更新CollectionViewSource

时间:2011-05-26 14:05:30

标签: wpf entity-framework data-binding collectionviewsource

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

它的样子如下: enter image description here

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

之后我使用了创建的数据源: enter image description here

“名称” - TextBox是ReadOnly。我想允许用户输入ID并在按下“搜索”按钮时搜索它。因此,如果可以找到指定的ID,则应更新“名称” - 文本框。我对DataGrids并不在乎,因为我在实际代码中不需要它们。

我不知道如何更新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);
    }
}

2 个答案:

答案 0 :(得分:1)

@ Freeze2046

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

  • MoveCurrentToFirst
  • MoveCurrentToLast
  • MoveCurrentToNext
  • MoveCurrentToPrevious
  • 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并对其作出反应。希望这会给你一些关于如何做到这一点的想法,但如果你需要,可以随意提出更多问题。

干杯,
Berryl

答案 1 :(得分:0)

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

您的ViewModel应如下所示:

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

string SearchText { get; set; }
ICommand SearchCommand { get; }