2014-07-01 104 views
0

在我的XAMLWPF DataContext的網格綁定

<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我有以下我有我變成的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> 

回答

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> 

現在您的代碼定義了集合的每個項目應該出現......我猜這是什麼你瞄準。

+0

嗯,我明白你在說什麼,雖然你有什麼建議沒有工作。我使用E.F和Datasets做了類似的事情,參見上文。我不確定爲什麼這種方法奏效,這不是? – David

+0

我在發佈之前測試了這段代碼,它*做了*工作。我不能告訴你爲什麼它不適合你...你必須有其他的東西干擾它。把它放在一個新的項目中,你會發現它*工作。 – Sheridan