Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DataGridView with DataGridTemplateColumns - Cannot create new rows
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="False" CanUserAddRows="True" Margin="0,50,0,0">
<DataGrid.Columns>
<!-- Browse -->
<DataGridTemplateColumn Header="Browse">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Browse}" Content="..." Width="40" Margin="6,0,6,0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Path -->
<DataGridTemplateColumn Width="300" Header="Path" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Slider -->
<DataGridTemplateColumn Width="100" Header="Slider">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Slider Value="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Minimum="1" Maximum="50" Margin="6,0,6,0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Slider's value -->
<DataGridTextColumn Binding="{Binding Value}" Header="Value" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PropertyChanged;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
}
public class RelayCommand : ICommand
{
public Action CallBack { get; set; }
public Predicate<object> Enable { get; set; }
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return Enable == null ? true : Enable(parameter);
}
public void Execute(object parameter = null)
{
CallBack();
}
}
[AddINotifyPropertyChangedInterface]
public class ViewModel
{
public ObservableCollection<Row> Rows { get; private set; }
public ViewModel()
{
Rows = new ObservableCollection<Row>();
//Rows.Add(new Row()); // Uncomment this line and the binding works
}
}
[AddINotifyPropertyChangedInterface]
public class Row
{
public RelayCommand Browse { get; private set; }
public string Path { get; private set; }
public double Value { get; set; }
public Row()
{
Browse = new RelayCommand();
Browse.CallBack = BrowseFile;
}
private void BrowseFile()
{
Path = System.IO.Path.GetRandomFileName(); // Some random string.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.