0

这似乎有点奇怪:我有一个带有按钮列的 Datagrid,当单击该行的按钮时,该列会删除该行。但是,如果我设置 Datagrid SelectionUnit="Cell" 则按钮列被禁用,我不能再单击该按钮。

谁能告诉我为什么会发生这种情况以及如何避免该列的禁用行为?

这是重新创建问题的 XAML - 添加和删除 SelectionUnit="Cell" 以查看行为变化

<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>

将此添加到 Window 后面的代码中:

public class Dummy
{
    public string x { get; set; }
    public string y { get; set; }
    public string z { get; set; }
}
public class DummyCollection : ObservableCollection<Dummy> { }
4

1 回答 1

1

我看不出设置SelectionUnit="Cell"或之间有什么区别SelectionUnit="FullRow"。两者都不起作用,因为命令属性未绑定到命令。

您应该将按钮的 Command 属性绑定到ICommand. 这是一个基于您的代码使用 MVVM Light 的 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; }
}
于 2012-06-15T22:33:16.623 回答