WPF DataContext网格绑定

时间:2014-07-01 07:23:39

标签: wpf

我的XAML中有以下内容

<UserControl.Resources>
    <vm:StaffViewModel x:Key="StaffViewModel" />
    <CollectionViewSource x:Key="StaffViewSource" Source="{Binding StaffMembers}" />
</UserControl.Resources>

    <Grid x:Name="StaffGrid" DataContext="{StaticResource StaffViewSource}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Label Content="FirstName:" Grid.Column="0" Grid.Row="0"/>
        <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding FirstName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
        <Label Content="Surname:" Grid.Row="1" VerticalAlignment="Center"/>
        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Surname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
    </Grid>

在我的ViewModel中,我有一个Observable对象集合,我将其转换为CollectionView

    private ObservableCollection<Staff> _staffMembers = null;
    private ICollectionView _staffMembersView = null;

    public ICollectionView StaffMembers
    {
        get { return _staffMembersView; }
    }


    public StaffView()
    {
        InitializeComponent();

        _staffMembers = GetStaff();
        _staffMembersView = CollectionViewSource.GetDefaultView(_staffMembers);

    }

但我的文本框中没有出现任何绑定

我错过了什么?

我过去使用EntityFramework和Datasets做过类似的事情。这就是我在那个场合定义的: -

<UserControl.Resources>
    <local:CantileverDataSet x:Key="CantileverDataSet"/>
    <CollectionViewSource x:Key="CabinetsViewSource" Source="{Binding Cabinets, Source={StaticResource CantileverDataSet}}"/>
</UserControl.Resources>
<Grid DataContext="{StaticResource CabinetsViewSource}">
      <!-- Individual Fields as textboxes here -->
</Grid>

1 个答案:

答案 0 :(得分:0)

您正在尝试将集合的CollectionViewSource数据绑定到单个控件,但您只能将其绑定到集合控件。试试这个:

<ListBox ItemsSource="{Binding Source={StaticResource StaffViewSource}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="StaffGrid">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Label Content="FirstName:" Grid.Column="0" Grid.Row="0" />
                <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding FirstName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
                <Label Content="Surname:" Grid.Row="1" VerticalAlignment="Center" />
                <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Surname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您的代码现在定义了该集合的每个项目应该出现的内容......我猜这就是您的目标。