简体   繁体   English

在Silverlight中将组合框添加到DataGrid

[英]Adding a Combobox to a DataGrid in Silverlight

I can add a Combobox to a DataGrid using following xmal: 我可以使用以下xmal将组合框添加到DataGrid:

 <local:DataGridTemplateColumn Header="SomeHeader" Width="106" HeaderStyle="{StaticResource headerAlignRightStyle}" CellStyle="{StaticResource cellAlignRightStyle}">
                    <local:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding SomeProp}" Margin="4"/>
                        </DataTemplate>
                    </local:DataGridTemplateColumn.CellTemplate>
                    <local:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox 
                              x:Name="SomeCombo"
                              SelectionChanged="SomeCombo_SelectionChanged"
                              ItemsSource="{Binding SomeList}"
                              DisplayMemberPath="Name" 
                              />
                        </DataTemplate>
                    </local:DataGridTemplateColumn.CellEditingTemplate>
                </local:DataGridTemplateColumn>

However what I can't figure out is a sensible way to get the row that was combox is bound to. 但是我不知道是一种明智的方法来获取被combox绑定的行。 ie when handling the combobox SelectionChanged event I have no way of knowing what what row the combobox belongs to. 即当处理组合框SelectionChanged事件时,我无法知道组合框属于哪一行。 Particularly I don't know what object in the DataGrid datasource that the combobox is refering to. 特别是我不知道组合框所指的是DataGrid数据源中的哪个对象。

Any help would be much appreciated. 任何帮助将非常感激。

you could 你可以

A) Bind the SelectedItem property of the ComboBox to a property in your ViewModel/data model using a two way binding, so you wouldn't have to worry about SelectionChanged in the first place A)使用双向绑定将ComboBox的SelectedItem属性绑定到ViewModel /数据模型中的属性,因此您不必首先担心SelectionChanged

or 要么

B) Use DataGridRow.GetRowContainingElement(element) in your SelectionChanged handler, ie B)在SelectionChanged处理函数中使用DataGridRow.GetRowContainingElement(element),即

private void SomeCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var comboBox = sender as ComboBox;
    if (comboBox == null)
        return;
    var row = DataGridRow.GetRowContainingElement(comboBox);
    // Do something with row...
}

Cheers, Alex 干杯,亚历克斯

If you are just looking to get the item the row is bound to, you can just read the DataContext of the sender: 如果您只是想获取该行绑定的项目,则可以读取发送者的DataContext:

private void SomeCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var item = sender as FrameworkElement;
    if (item== null)
        return;
    var source = item.DataContext;
}

As I understand it, when you are clicking on the combo box, that row should get focus. 据我了解,当您单击组合框时,该行应获得焦点。 This also means that the datagrid is aware of the selected item. 这也意味着数据网格知道所选项目。

If you are looking for the selected object, you should have access to it with datagridName.SelectedItem . 如果要查找选定的对象,则应该使用datagridName.SelectedItem对其进行访问。 This will return the selected object. 这将返回所选对象。

Please test it and comment on the solution as I am unable to check the answer right now. 请对其进行测试,并对解决方案发表评论,因为我现在无法检查答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM