[英]WPF DataGrid Remove Column
I have created a DataGrid in WPF from a DataTable with AutoGenerateColumns = true
. 我已经使用
AutoGenerateColumns = true
从DataTable在WPF中创建了DataGrid。 In the VM I have a command property for adding and deleting columns which manipulates the underlying DataTable. 在VM中,我具有用于添加和删除操作基础DataTable的列的command属性。
When I call AddColumn
from the main XAML window via Command="{Binding AddColumn}"
it works as expected but when I call the RemoveColumn from a resource file with a context menu it calls the command property (im able to step through the code) but does not update the Grid. 当我通过
Command="{Binding AddColumn}"
从XAML主窗口调用AddColumn
,它可以按预期工作,但是当我从带有上下文菜单的资源文件中调用RemoveColumn时,它将调用command属性(可以逐步执行代码)但不会更新网格。
<MenuItem Header="Delete" Command="{Binding Source={StaticResource AppViewModel}, Path=DeleteColumn}" CommandParameter="{Binding}" />
I now updated both commands to only set the DataTable to null, ie Dt = null
and for the AddColumn this works as expected and removed the Grid and columns but for the RemoveColumn it does nothing... I also see no errors in the output window in regard to binding and when stepping through the code the property is called. 现在,我更新了这两个命令,仅将DataTable设置为null,即
Dt = null
,对于AddColumn可以按预期工作,并删除了Grid和列,但是对于RemoveColumn则不执行任何操作……我也看不到输出窗口中的错误关于绑定以及单步执行代码时,将调用该属性。 I also tried to set the column to invisible which also did not work. 我还尝试将列设置为不可见,这也不起作用。
UPDATE UPDATE
I call the DeleteColumn from the following (simplified) code in a resources file. 我从资源文件中的以下(简化)代码中调用DeleteColumn。
<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Style.Triggers>
<DataTrigger Binding="{Binding Content, ConverterParameter=*, Converter={StaticResource AfterDashConverter}, RelativeSource={RelativeSource self}}" Value="Green">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete" Command="{Binding Source={StaticResource AppViewModel}, Path=DeleteColumn}" />
</ContextMenu>
</Grid.ContextMenu>
<DataGridColumnHeader x:Name="PART_FillerColumnHeader" IsHitTestVisible="False"/>
<ItemsPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
The commands are very simple, the AddColumn works as its directly in my main view and the DeleteColumn does not as its in the above resource file. 这些命令非常简单,AddColumn可以直接在主视图中使用,而DeleteColumn则不可以在上述资源文件中使用。 I have checked the commands fire properly.
我检查了命令是否正确触发。
AddColumn = new RelayCommand(_ =>
{
Dt = null;
}, true);
DeleteColumn = new RelayCommand(column =>
{
Dt = null;
}, true);
the main issue was my resource was creating a new viewmodel so it was not calling / impacting the VM instance that my view was dependent on. 主要问题是我的资源正在创建一个新的视图模型,因此它不会调用/影响我的视图所依赖的VM实例。 To fix this I needed to set the
DataContext
of my ContextMenu
parent control (which was a grid) as follows: 为了解决这个问题,我需要如下设置
ContextMenu
父控件(是网格)的DataContext
:
DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyType}}}
If you just call the following it will create a new instance of your ViewModel and use that: 如果仅调用以下命令, 它将创建一个ViewModel的新实例并使用它:
<MenuItem Header="Delete" Command="{Binding Source={StaticResource AppViewModel}, Path=DeleteColumn}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.