3

I want to use the DataGridComboBoxColumn as a autocomplete combobox.

I've got it partially working. When the Row is in EditMode I can type text in the ComboBox, also in ViewMode the control returns the text. Only how to get the Label (in template) to EditMode by mouse doubleclick?

Up front, I don't want to use the DataGridTemplateColumn control because it just doesn't handle keyboard and mouse entry like the DataGridComboBoxColumn does (tabs, arrows, edit/view mode/ double click etc..).

It looks like:

app

1 Answer 1

3

I fixed it adding a behavior to the TextBox to get a link to the parent DataGrid then setting the Row into Edit Mode by calling BeginEdit().

The solution I used:

View

<Window x:Class="WpfApplication1.MainWindow"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <local:BindingProxy x:Key="proxy" Data="{Binding}" />
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{Binding Model.Things}" Name="MyGrid" ClipboardCopyMode="IncludeHeader">
            <DataGrid.Resources>

            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Object" MinWidth="140" TextBinding="{Binding ObjectText}" ItemsSource="{Binding Source={StaticResource proxy}, Path=Data.Model.ObjectList}" >
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="IsEditable" Value="True"/>
                            <Setter Property="Text" Value="{Binding ObjectText}"/>
                            <Setter Property="IsSynchronizedWithCurrentItem" Value="True" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <TextBox IsReadOnly="True" Text="{Binding Path=DataContext.ObjectText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}">
                                            <TextBox.Resources>
                                                <Style TargetType="{x:Type TextBox}">
                                                    <Setter Property="local:CellSelectedBehavior.IsCellRowSelected" Value="true"></Setter>
                                                </Style>
                                            </TextBox.Resources>
                                        </TextBox>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Model

public class Model : BaseModel
{
    //List of objects for combobox
    private List<string> _objectList;
    public List<string> ObjectList { get { return _objectList; } set { _objectList = value; } }

    //Rows in datagrid
    private List<Thing> _things;
    public List<Thing> Things
    {
        get { return _things; }
        set { _things = value; OnPropertyChanged("Things"); }
    }
}

public class Thing : BaseModel
{
    //Text in combobox
    private string _objectText;
    public string ObjectText
    {
        get { return _objectText; }
        set { _objectText = value; OnPropertyChanged("ObjectText"); }
    }
}

ViewModel

public class ViewModel
{
    public Model Model { get; set; }

    public ViewModel()
    {
        Model = new WpfApplication1.Model();
        Model.ObjectList = new List<string>();
        Model.ObjectList.Add("Aaaaa");
        Model.ObjectList.Add("Bbbbb");
        Model.ObjectList.Add("Ccccc");
        Model.Things = new List<Thing>();
        Model.Things.Add(new Thing() { ObjectText = "Aaaaa" });
    }
}

Behavior

public class CellSelectedBehavior
{
    public static bool GetIsCellRowSelected(DependencyObject obj) { return (bool)obj.GetValue(IsCellRowSelectedProperty); }

    public static void SetIsCellRowSelected(DependencyObject obj, bool value) { obj.SetValue(IsCellRowSelectedProperty, value); }

    public static readonly DependencyProperty IsCellRowSelectedProperty = DependencyProperty.RegisterAttached("IsCellRowSelected",
      typeof(bool), typeof(CellSelectedBehavior), new UIPropertyMetadata(false, OnIsCellRowSelected));

    static void OnIsCellRowSelected(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TextBox item = depObj as TextBox;
        if (item == null)
            return;

        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
            item.MouseDoubleClick += SelectRow;
        else
            item.MouseDoubleClick -= SelectRow;
    }

    static void SelectRow(object sender, EventArgs e)
    {
        TextBox box = sender as TextBox;
        var grid = box.FindAncestor<DataGrid>();
        grid.BeginEdit();
    }
}

Helper (to find DataGrid)

public static class Helper
{
    public static T FindAncestor<T>(this DependencyObject current) where T : DependencyObject
    {
        current = VisualTreeHelper.GetParent(current);
        while (current != null)
        {
            if (current is T)
            {
                return (T)current;
            }
            current = VisualTreeHelper.GetParent(current);
        };
        return null;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.