如何在datagrid中打开组合框

时间:2012-10-25 09:59:53

标签: silverlight datagrid combobox

我对silverlight应用程序很新。我一直在尝试这一天仍然无法做到。在我的情况下,需要三次点击才能打开组合框。

我的XAML代码:

<sdk:DataGridTemplateColumn Header="Category" Width="100">
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate> 
            <ComboBox Name="cmbCategory"
                IsDropDownOpen="True" 
                ItemsSource="{Binding Source={StaticResource MyViewModel},Mode=OneWay,Path=pcProjTypeTaskCtry}" 
                DisplayMemberPath="TaskCtgyName" SelectedValuePath="TaskCtgy_FK" 
                SelectedValue="{Binding piTaskCtgy_FKField,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ToolTipService.ToolTip="{Binding psTaskCtgyNameField,UpdateSourceTrigger=PropertyChanged}"/> 
        </DataTemplate>

    </sdk:DataGridTemplateColumn.CellTemplate> 
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="SubCategory" Width="110">
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Name="tbSubCategory" Text="{Binding Path=psTaskSubCtgyNameField,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ToolTipService.ToolTip="{Binding psTaskSubCtgyNameField}" >
            </TextBlock>
        </DataTemplate> 
    </sdk:DataGridTemplateColumn.CellTemplate>
    <sdk:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Source={StaticResource MyViewModel},Mode=TwoWay,Path=pcTaskSubCtry}" 
                DisplayMemberPath="TaskSubCtgyName" SelectedValuePath="TaskSubCtgyName" 
                SelectedItem="{Binding Source={StaticResource MyViewModel},Path=SelectedSubTask,Mode=TwoWay}" 
                SelectedValue="{Binding psTaskSubCtgyNameField,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                ToolTipService.ToolTip="{Binding psTaskSubCtgyNameField}"> 
            </ComboBox>
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>

1 个答案:

答案 0 :(得分:0)

将Dropdownopened事件添加到组合框,然后将数据源分配给后端的组合框。

        var obj = (ComboBox)sender;
        obj.ItemsSource = Your list;
        obj.DisplayMemberPath = "pcTaskSubCtry";

在选择更改事件中,您可以通过投射所选项目来指定所选项目,如下所示。

 ComboBox cmbbox = (ComboBox)sender;
        if (cmbbox.SelectedValue != null)
        {
            Yourobject obj = new Yourobject() ;
            obj = MainDataGrid.SelectedItem as Yourobje;
            obj.pcTaskSubCtry= Yourobje.pcTaskSubCtry;
        }