单击按钮将新行添加到DataGrid

时间:2019-07-11 09:35:57

标签: c# wpf data-binding datagrid

我是WPF的新手,在单击按钮时向Data-Grid添加新行时遇到问题。我尝试了几种解决方案,但没有成功。

尝试过- 如果我启用“ CanUserAddRows =“ True”,这还会为我提供位于上排的按钮。我也不希望这种方法,因为我不想在单击按钮时添加新行。

I am attaching an image of how new row looks like if i use "CanUserAddRows="True"

<StackPanel Name="Decrypt_Vault">
                            <DataGrid  x:Name="gdDecryptVault" HorizontalAlignment="Left"  ColumnWidth="Auto" Margin="10,10,405,18"  AutoGenerateColumns="False" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray">

                                <DataGrid.Columns>
                                    <!--<DataGridTextColumn Header="Host" Binding="{Binding Path=Host}" Width="*" IsReadOnly="True" />-->
                                    <DataGridTemplateColumn Header="Host">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=Host}"></TextBlock>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                        <DataGridTemplateColumn.CellEditingTemplate>
                                            <DataTemplate>
                                                <TextBox Background="Aquamarine" Text="{Binding Path=Host, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellEditingTemplate>
                                    </DataGridTemplateColumn>

                                    <!--<DataGridTextColumn Header="Login" Binding="{Binding Path=Login}" Width="*" IsReadOnly="True" />-->
                                    <DataGridTemplateColumn Header="Login">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=Login}"></TextBlock>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                        <DataGridTemplateColumn.CellEditingTemplate>
                                            <DataTemplate>
                                                <TextBox Background="Aquamarine" Text="{Binding Path=Login, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellEditingTemplate>
                                    </DataGridTemplateColumn>

                                    <!--<DataGridTextColumn Header="Password" Binding="{Binding Path=Password}" Width="*" IsReadOnly="True"/>-->
                                    <DataGridTemplateColumn Header="Password" IsReadOnly="True">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                    <TextBlock Name="LinePassword" Text="{Binding Path=Password}" Visibility="{Binding Path=IsPasswordVisible, Converter={StaticResource BoolToVis}}"></TextBlock>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                        <DataGridTemplateColumn.CellEditingTemplate>
                                            <DataTemplate>
                                                <TextBox Background="Aquamarine" Text="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellEditingTemplate>
                                    </DataGridTemplateColumn>

                                    <DataGridTemplateColumn Header="Password Actions" IsReadOnly="True">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <DockPanel>
                                                    <Button  Click="bTogglePassword_Click" ToolTip="Toggle Password"
                                                        Name="bTogglePassword" Visibility="Visible"  Height="30" Width="30" >
                                                        <Image Source="/Images/button_login1.png" Stretch="Fill" Height="30" Width="30"/>
                                                    </Button>
                                                    <Button Click="bCopyToClipBoard_Click" ToolTip="Copy Password to clipboard"
                                                        Name="bCopyToClipBoard" Visibility="Visible"  Height="30" Width="30" >
                                                        <Image Source="/Images/Copy_icon.png" Stretch="Fill" Height="30" Width="30"/>
                                                    </Button>
                                                </DockPanel>
                                                <!--<TextBlock Text="{Binding Path=Password}"></TextBlock>-->
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>

                                    <!--<DataGridTextColumn Header="Items" Binding="{Binding Path=Project.Name}" Width="*" IsReadOnly="True"/>-->
                                    <DataGridTemplateColumn Header="Items" Width="100">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=Project.Name}"></TextBlock>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                        <DataGridTemplateColumn.CellEditingTemplate>
                                            <DataTemplate>
                                                <TextBox Background="Aquamarine" Text="{Binding Path=Project.Name, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellEditingTemplate>
                                    </DataGridTemplateColumn>

                                    <!--<DataGridTextColumn Header="Created by" Binding="{Binding Path=CreatedBy}" Width="*" IsReadOnly="True"/>-->
                                    <DataGridTemplateColumn Header="Created By" >
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=CreatedBy}"></TextBlock>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                        <DataGridTemplateColumn.CellEditingTemplate>
                                            <DataTemplate>
                                                <TextBox Background="Aquamarine" Text="{Binding Path=CreatedBy, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellEditingTemplate>
                                    </DataGridTemplateColumn>

                                    <!--<DataGridTextColumn Header="Notes" Binding="{Binding Path=Notes}" Width="*" IsReadOnly="True"/>-->
                                    <DataGridTemplateColumn Header="Notes" Width="100">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=Notes}"></TextBlock>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                        <DataGridTemplateColumn.CellEditingTemplate>
                                            <DataTemplate>
                                                <TextBox Background="Aquamarine" Text="{Binding Path=Notes, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellEditingTemplate>
                                    </DataGridTemplateColumn>

                                    <DataGridTemplateColumn Header="Actions" Width="100">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <DockPanel>
                                                    <!--<Button Content="Edit" Click="EditVaultLine"/>-->
                                                    <Button  Click="EditVaultLine" ToolTip="Edit"
                                                        Name="Edit" Visibility="Visible"  Height="30" Width="30" >
                                                        <Image Source="/Images/edit.png" Stretch="Fill" Height="30" Width="30"/>
                                                    </Button>
                                                    <Button  Click="DeleteVaultLine" ToolTip="Delete"
                                                        Name="Delete" Visibility="Visible"  Height="30" Width="30" >
                                                        <Image Source="/Images/delete.png" Stretch="Fill" Height="30" Width="30"/>
                                                    </Button>
                                                </DockPanel>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                        <DataGridTemplateColumn.CellEditingTemplate>
                                            <DataTemplate>
                                                <DockPanel >
                                                    <Button  Click="SaveEditedVaultLine" ToolTip="Save"
                                                        Name="Save" Visibility="Visible"  Height="30" Width="30" >
                                                        <Image Source="/Images/save2.png" Stretch="Fill" Height="30" Width="30"/>
                                                    </Button>
                                                    <Button  Click="CancelEditVaultLine" ToolTip="Cancel"
                                                        Name="Cancel" Visibility="Visible"  Height="30" Width="30" >
                                                        <Image Source="/Images/erase.png" Stretch="Fill" Height="30" Width="30"/>
                                                    </Button>
                                                </DockPanel>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellEditingTemplate>
                                    </DataGridTemplateColumn>

                                </DataGrid.Columns>
                            </DataGrid>
                        </StackPanel>



My .CS code to Bind data in Data-Grid
 var result = weekTaskView.getVaultRecordLines();
                List<VaultRecordLine> list = new List<VaultRecordLine>();
                foreach (var item in result.Entities)
                {
                    VaultRecordLine vrl = new VaultRecordLine();

                    if (item.Attributes.Contains("createdby"))
                    {
                        vrl.CreatedBy = item.Attributes["createdby"].ToString();
                    }
                    if (item.Attributes.Contains("new_account"))
                    {
                        vrl.Host = item.Attributes["new_account"].ToString();
                    }
                    if (item.Attributes.Contains("new_login"))
                    {
                        vrl.Login = item.Attributes["new_login"].ToString();
                    }
                    if (item.Attributes.Contains("new_password"))
                    {
                        vrl.Password = item.Attributes["new_password"].ToString();
                    }
                    if (item.Attributes.Contains("new_vaultid"))
                    {
                        vrl.Id = new Guid(item.Attributes["new_vaultid"].ToString());
                    }

                    list.Add(vrl);
                }
                gdDecryptVault.ItemsSource = list;

要求-当我单击“添加新行按钮”时,我需要处于“可编辑”模式的新行,以便用户可以填写数据。我还需要在行的末尾使用“保存”和“取消”按钮来保存该数据或分别“取消”它。

1 个答案:

答案 0 :(得分:0)

我制作了一个小示例应用程序,向您展示如何向DataGrid添加项目。

您的DataGrid应该绑定到ObservableCollection<T>,其中T是您的DataClass的类型。 DataGrid的定义如下:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}" CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Host" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock VerticalAlignment="Center" Margin="4,2" Text="{Binding Host, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Login" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock VerticalAlignment="Center" Margin="4,2" Text="{Binding Login, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Password" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock VerticalAlignment="Center" Margin="4,2" Text="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

然后,您有一个Button来向DataGrid的ItemsSource添加新行。 Button看起来像:

<Button Grid.Row="1" Content="Add item" Command="{Binding AddItemCommand}"/>

在您的C#代码中,将Button的命令绑定到的AddItemCommand看起来像:

private ICommand addItemCommand;
public ICommand AddItemCommand
{
    get { return addItemCommand ?? (addItemCommand = new RelayCommand(AddItem)); }
}

private void AddItem(object obj)
{
    Items.Add(new MyItem());
}

ObservableCollection<T>绑定的DataGrid类似:

private ObservableCollection<MyItem> items;

public ObservableCollection<MyItem> Items
{
    get { return items ?? (items = new ObservableCollection<MyItem>()); }
}

现在,当您按下按钮时,将执行AddItemCommand,这会将新的项目添加到DataGrid的ItemsSource。


RelayCommand是ICommand接口的实现。因此,您不必使用点击事件。因此,您可以将XAML定义(视图)与C#-Logic(视图模型)分离。这是使用MVVM模式的重要一步。

public class RelayCommand : ICommand
{
    private readonly Action<object> execute;
    private readonly Predicate<object> canExecute;

    public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
    {
        if(execute == null)
            throw new ArgumentException(nameof(execute));
        this.execute = execute;
        this.canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return canExecute == null || canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        execute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}

如果您不熟悉WPF,则应查看MVVM模式。真的很有用