如何在Devexpress GridControl

时间:2016-01-12 04:10:11

标签: c# wpf datagrid devexpress gridcontrol

我正在研究devexpress GridControl。我在GridColumn的EditTemplate中有一列。我如何在C#中引用Combobox。

我尝试使用EditTemplate.LoadContent(),但它给了我新的实例。我想要相同的实例,所以我可以改变一些值。

非常感谢任何帮助。

 <dxg:GridColumn.CellTemplate>
           <DataTemplate>
              <controls:ComboBox Name="PART_Editor" ItemsSource="{Binding RowData.Row.TransportModeTypes}" DisplayMember="Value" ValueMember="Key" Background="Transparent" ToolTip="Enter Transport Mode" />
            </DataTemplate>
   </dxg:GridColumn.CellTemplate>

1 个答案:

答案 0 :(得分:2)

您始终可以直接在Visual Tree中找到您的ComboBox。像这样:

    private IEnumerable<T> GetVisualChildren<T>(DependencyObject parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;

            if (child == null)
            {
                foreach (var item in GetVisualChildren<T>(v))
                {
                    yield return item;
                }
            }

            if (child != null)
            {
                yield return child;
            }
        }

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        GetVisualChildren<ComboBox>(dataGrid).ToList().ForEach(c => c.SelectedItem  = null);
    }

或者您可以实施并附加行为:

public class MyComboBoxBehavior : Behavior<GridViewBase>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        GridControl gridControl = AssociatedObject.Grid;

        // Logic for combobox handling goes here
    }
}


<dxg:GridControl>
        <dxg:GridControl.View>
            <dxg:TableView>
                <i:Interaction.Behaviors>
                    <local:MyComboBoxBehavior/>
                </i:Interaction.Behaviors>
            </dxg:TableView>
        </dxg:GridControl.View>
    </dxg:GridControl>