2016-03-03 135 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加载时。 有什么想法?

相关问题