How to make datagrid see editing in a control?

I have a user control that I use to populate a datagrid.

I would like the user to be able to add items by editing the empty line at the bottom. (This is why I use a datagrid, not an itemcontrol). However, the datagrid does not understand that the last item is being edited if the user does not click on the background of the control. I would like a new item to be added when the user makes changes to the properties that the control provides.

XAML control:

<UserControl x:Class="ControlTest.MyControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ControlTest"
         mc:Ignorable="d" 
         d:DesignHeight="50" d:DesignWidth="300"
         DataContext="{Binding Path=., Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
         >
<StackPanel Orientation="Vertical">
        <TextBox Text="{Binding Path=p1, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
           Width="300"
           Height="30"
           VerticalAlignment="Center"/>
    <ComboBox ItemsSource="{Binding Path=DropDownValues,
              RelativeSource={RelativeSource Mode=FindAncestor,
                AncestorType=local:MyControl}}"
              SelectedItem="{Binding Path=p2, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
              Height="30"/>
    </StackPanel>
</UserControl>

CS:

public partial class MyControl : UserControl
{
    private static readonly DependencyProperty DropDownValuesProperty =
        DependencyProperty.Register(
        "DropDownValues", 
        typeof(List<String>),
        typeof(MyControl),
        new FrameworkPropertyMetadata(new List<String>()
        ));
    public List<String> DropDownValues
    {
        get
        {
            return (List<String>)GetValue(DropDownValuesProperty);
        }
        set
        {
            SetValue(DropDownValuesProperty, value);
        }
    }

    public MyControl()
    {
        InitializeComponent();
    }        
}

DataGrid XAML

    <DataGrid 
        AutoGenerateColumns="False" 
        ItemsSource="{Binding objs, Mode=TwoWay}" 
        HeadersVisibility="None"
        Margin="0,0,0.4,0"
        CanUserAddRows="True"
        >
        <DataGrid.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </DataGrid.ItemsPanel>
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="300">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate DataType="local:Measure">
                        <local:MyControl 
                            DataContext="{Binding ., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            DropDownValues=
                        "{Binding DataContext.list, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                            Width="300"
                        />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

Can I do this work and / or is there a better way to do this?

+4
source share
2

:

CanUserAddRows=false DataGrid, ObservableCollection<Something>, DataGrid.

- , :

xaml:

<DataGrid x:Name="myDataGrid" CellEditEnding="DataGrid_CellEditEnding" .....>
    <!--Some Code-->
</DataGrid>

Code-Behind:

private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    myDataGrid.CommitEdit();
}

, .

:

DataGrid Beginning :

private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    if ((selectedRow as DataGridRow).Item.ToString() != "{NewItemPlaceholder}")
    {
        //Here you can add the code to add new item. I don't know how but you should figure out a way
    }
}

. , , .

:

DataGrid. ListBox. . , . ListBox , , datagrid. : https://drive.google.com/open?id=0B5WyqSALui0bTXFGZWxQUWVRdkU

+2

, objs? , objs, objs . , objs INotifyPropertyChanged p1 p2 OnPorpertyChanged, .

public ObservableCollection<YourObject> objs

public class YourObject : INotifyPropertyChanged
{
    protected void OnPropertyChanged(string Name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(Name));
        }
    }

    private string _p1;
    public string p1
    {
        get { return _p1; }
        set
        {
            if (_p1 != value)
            {
                _p1 = value;
                OnPropertyChanged("p1");
            }
        }
    }
    private string _p2;
    public string p2
    {
        get { return _p2; }
        set
        {
            if (_p2 != value)
            {
                _p2 = value;
                OnPropertyChanged("p2");
            }
        }
    }
}
+1

All Articles