0

This seems a bit bizzare: I have a Datagrid with a button column which deletes the row when the button for that row is clicked. BUT if I set the Datagrid SelectionUnit="Cell" then the button column is disabled and I can no longer click the button.

Can anyone tell me why this happens and how to avoid the disabling behaviour for the column?

Here is XAML that recreates the issue - add & remove the SelectionUnit="Cell" to see the change in behaviour

<Window.Resources>
    <local:DummyCollection x:Key="stringCollection">
        <local:Dummy x="1" y="2" z="3" />
        <local:Dummy x="4" y="5" z="6" />
        <local:Dummy x="7" y="8" z="9" />
    </local:DummyCollection>
</Window.Resources>

<StackPanel>
    <DataGrid ItemsSource="{Binding Source={StaticResource stringCollection}}" AutoGenerateColumns="False" SelectionUnit="Cell">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="a" Binding="{Binding Path=x}" Header="a" />
            <DataGridTextColumn x:Name="b" Binding="{Binding Path=y}" Header="b" />
            <DataGridTextColumn x:Name="c" Binding="{Binding Path=z}" Header="c" />
            <DataGridTemplateColumn  x:Name="deleteButtonColumn" Header="D">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Command="Delete" >D</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

Add this to the Window's code behind:

public class Dummy
{
    public string x { get; set; }
    public string y { get; set; }
    public string z { get; set; }
}
public class DummyCollection : ObservableCollection<Dummy> { }
Gary Barrett
  • 1,764
  • 5
  • 21
  • 33

1 Answers1

1

I don't see a difference between setting SelectionUnit="Cell" or SelectionUnit="FullRow". Neither work because the command property isn't binding to a command.

You should be binding the Command property of your button to an implementation of ICommand. Here's an example based on your code using MVVM Light's RelayCommand.

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
<StackPanel>
    <DataGrid ItemsSource="{Binding Items}" 
              AutoGenerateColumns="False" SelectionUnit="Cell">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="a" Binding="{Binding Path=x}" Header="a" />
            <DataGridTextColumn x:Name="b" Binding="{Binding Path=y}" Header="b" />
            <DataGridTextColumn x:Name="c" Binding="{Binding Path=z}" Header="c" />
            <DataGridTemplateColumn  x:Name="deleteButtonColumn" Header="D">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Command="{Binding Path=DataContext.Delete, 
                                RelativeSource={RelativeSource Mode=FindAncestor, 
                                AncestorType={x:Type DataGrid}}}" 
                                CommandParameter="{Binding}">D</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

public class Dummy
{
    public string x { get; set; }
    public string y { get; set; }
    public string z { get; set; }

    public override string ToString()
    {
        return string.Format("x:{0}, y:{1}, z:{2}", x, y, z);
    }
}

public class DummyCollection : ObservableCollection<Dummy> { } 

public class ViewModel
{
    public ViewModel()
    {
        Items = new DummyCollection
            {
                new Dummy {x = "1", y = "2", z = "3"},
                new Dummy {x = "4", y = "5", z = "6"},
                new Dummy {x = "7", y = "8", z = "9"},
            };

        Delete = new RelayCommand<Dummy>(DeleteItem);
    }

    public ICommand Delete { get; private set; }

    private void DeleteItem(Dummy item)
    {
        Debug.WriteLine("Delete item, " + item);
    }

    public DummyCollection Items { get; private set; }
}
Phil
  • 42,255
  • 9
  • 100
  • 100