2016-09-16 45 views
0

在搜索關於此主題的幫助時,我通常會找到有關如何用相關數據填充ComboBox的信息(在原始表中爲外鍵顯示一個表中的顯示名稱) ,但我已經能夠做到這一點。我正在尋找如何在TextBlock中實現相同結果的解決方案。WPF如何在ComboBox中顯示DataGrid的TextBlock中的關係數據

在我的項目中,我創建了一個使用EntityFramework的SQL數據庫的鏈接。 有兩個表格:人員和角色。 它們與Personnel.RoleIdFk和Role.RoleId有關。

Related Tables

我能夠得到一個組合框來顯示Role.RoleName的相關RoleIdFk。

ComboBoxes

在我的ViewModel:

private ICollection<Role> roleList; 
    public ICollection<Role> RoleList 
    { 
     get { return roleList; } 
     set { roleList = value; NotifyPropertyChanged("RoleList"); } 
    } 

    private Role selectedRole; 
    public Role SelectedRole 
    { 
     get { return selectedRole; } 
     set { selectedRole = value; NotifyPropertyChanged("SelectedRole"); } 
    } 

    public void SetSelectedRole() 
    { 
     if (SelectedPerson == null) 
     { 
      SelectedPerson = PersonnelList.Select(p => p) 
              .FirstOrDefault(); 
     } 


     SelectedRole = RoleList.Where(p => p.RoleId == SelectedPerson.RoleIdFk) 
            .FirstOrDefault(); 
    } 

而且XAML組合框:

 <ComboBox x:Name="cboRole" 
       Grid.Column="1" Grid.Row="0" 
       Width="150" Height="35" 
       ItemsSource="{Binding RoleList}" 
       DisplayMemberPath="RoleName" 
       SelectedItem="{Binding SelectedRole}" 
       /> 

我的問題是我還顯示一個DataGrid的人員表。 在這個DataGrid中,我希望顯示相關的RoleName而不是數字的外鍵。
目前XAML爲DataGrid:

<Window.DataContext> 
    <viewModel:MainWindowVM /> 
</Window.DataContext> 

    <ListView Name="lstPersonnel" 
       Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" 
       ItemContainerStyle="{StaticResource ItemContStyle}" 
       ItemsSource="{Binding PersonnelList}" > 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Width="75" Header="First Name" > 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding FirstName}" /> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 

       <GridViewColumn Width="75" Header="Last Name" > 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding LastName}" /> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 

       <GridViewColumn Width="75" Header="Role" > 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding RoleIdFk}" /> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
      </GridView> 
     </ListView.View> 
    </ListView> 

這是網格的樣子:

DataGrid

對於最後一列,我試圖改變以各種方式文本字段。 我也嘗試過使用其他模板和樣式。這些都沒有成功。

是否有一種方法可以在XAML本身中實際執行此操作,或者我將不得不設置某種類型的值轉換器來完成此操作?

注:我曾想過使用某種「開關/外殼」場景。雖然它可以在這裏工作,但我需要找出一些更具活力的東西。在項目後期,我需要將PersonnelId與其他項目相關聯。 300多人使用「開關/箱子」方案是不可行的。

回答

0

「角色」應該在「人員」對象中可用。因此,你可以簡單地用「Role.RoleName」而不是「RoleIdFk」:

<TextBlock Text="{Binding Role.RoleName}" /> 

的情況下,當你沒有一個參考的對象,最好是加入兩個集合,並使用結果作爲的一個的ItemsSource數據網格。這裏有一個例子: Joining two tables in one datagrid usig Linq C# WPF

還擁有IMultiValueConverter一個解決方案,但似乎過於複雜: Joining 2 collections into datagrid using multibinding

+0

OMG。我想我的問題是我沒有遵循K.I.S.S.校長,我試圖讓它過於複雜。這工作完美。 – Cedage

相關問題