2016-03-03 83 views
0

我有一個問題,我正在將對象的集合綁定到ComboBox(單元格在DataGrid中)。在DataGrid中綁定對象到ComboBox的集合SilverLight

我在網格組合框,但它是空的,沒有數據(該集合不爲空):

<sdk:DataGrid Name="CdnsDataGrid" AutoGenerateColumns="False" Grid.Column="1" Grid.Row="1" Height="200" RowHeight="40" Margin="0,20,30,20" RowEditEnded="LinesDataGrid_RowEditEnded"> 
     <sdk:DataGrid.Columns> 
     <sdk:DataGridTemplateColumn Width="*" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Welcome Message"> 
       <sdk:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 

         <ComboBox Name="VoiceMessagesComboBox" SelectedIndex="3" Width="250" ItemsSource="{Binding VMCollection}" SelectionChanged="OnVMSelectionChanged"> 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <Grid Width="200" Height="46"> 
             <TextBlock HorizontalAlignment="Center" Text="{Binding Description}"/> 
            </Grid> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 

        </DataTemplate> 
       </sdk:DataGridTemplateColumn.CellTemplate> 
       <sdk:DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 

        </DataTemplate> 
       </sdk:DataGridTemplateColumn.CellEditingTemplate> 

      </sdk:DataGridTemplateColumn> 
      </sdk:DataGrid.Columns> 
      </sdk:DataGrid>  
</Grid> 

收集項目有:ID,路徑說明:

我'試圖顯示在網格中的組合框項目的描述,並在「保存」點擊,獲得項目的ID(按行)

任何想法,如何解決它? 提前致謝。

回答

1

由於ComboBox在DataGrid中,因此它位於該DataGrid的上下文中。看起來你正在訪問ViewModel上的一個集合,所以你需要指定源代碼。

一種選擇是在用戶控件上將視圖模型指定爲資源並將其指定爲ComboBox ItemSource的源。

xmlns:vm="clr-namespace:My.App.ViewModels" 


<UserControl.Resources> 
    <vm:MyViewModel x:Key="myViewModel"/> 
</UserControl.Resources> 

<sdk:DataGrid Name="CdnsDataGrid" AutoGenerateColumns="False" Grid.Column="1" Grid.Row="1" Height="200" RowHeight="40" Margin="0,20,30,20" RowEditEnded="LinesDataGrid_RowEditEnded"> 
     <sdk:DataGrid.Columns> 
     <sdk:DataGridTemplateColumn Width="*" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Welcome Message"> 
       <sdk:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 

         <ComboBox Name="VoiceMessagesComboBox" SelectedIndex="3" Width="250" ItemsSource="{Binding VMCollection, Source={StaticResource myViewModel}}" SelectionChanged="OnVMSelectionChanged"> 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <Grid Width="200" Height="46"> 
             <TextBlock HorizontalAlignment="Center" Text="{Binding Description}"/> 
            </Grid> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 

        </DataTemplate> 
       </sdk:DataGridTemplateColumn.CellTemplate> 
       <sdk:DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 

        </DataTemplate> 
       </sdk:DataGridTemplateColumn.CellEditingTemplate> 

      </sdk:DataGridTemplateColumn> 
      </sdk:DataGrid.Columns> 
</sdk:DataGrid> 

如果您是通過代碼隱藏設置您的DataContext,這種做法是行不通的。在這種情況下,您將需要執行元素綁定。

+0

謝謝。我發佈了更新後的答案。 – David

0

ComboBox將當前收集項目('行')作爲數據上下文,但收集位於VM中。你可以爲你的綁定指定相源,並將其指向父DataGrid中(衛生組織數據上下文爲VM):

<ComboBox Name="VoiceMessagesComboBox" SelectedIndex="3" Width="250" 
      ItemsSource="{Binding DataContext.VMCollection, RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:DataGrid}}" 
      SelectionChanged="OnVMSelectionChanged"> 
+0

謝謝。我發佈了更新後的答案。 – David

0

感謝,最後,這是我做了,而且它的工作原理:

<ComboBox Height="23" Name="cbxFuelType" SelectionChanged="cbxFuelType_SelectionChanged" ItemsSource="{StaticResource VMCollection}">              
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <Grid Width="200" Height="46"> 
             <TextBlock HorizontalAlignment="Center" Text="{Binding Description,Mode=TwoWay}"/> 
            </Grid> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 

現在,我需要按ID顯示The Description,當DataGrid加載時。 有什麼想法?