我正在尝试在数据网格的顶部添加一个导出按钮。我最大的问题是能够添加commandParameter,以及所有datagrid的选定项。
我已经为contextMenu做了,但是我需要添加一个按钮来做同样的想法。
这是代码:
<Grid>
<Controls:MetroContentControl>
<Expander Header="HeaderExpa" VerticalAlignment="Top" Grid.Row="1" IsExpanded="True" IsEnabled="True">
<Grid Name="Namegrid">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding ressource}" Name="NameDatagrid">
<DataGrid.Resources>
<Style x:Key="AlignBottomColumnHeader" BasedOn="{StaticResource AzureDataGridColumnHeader}" TargetType="DataGridColumnHeader">
<Setter Property="VerticalContentAlignment" Value="Bottom" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<!--Description-->
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem
Header="HeaderContextMenu"
Command="{ Binding CommandExport }"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}"
/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
</Grid>
</Expander>
</Controls:MetroContentControl>
</Grid>
如何将此特定按钮添加到我的数据网格?我尝试了一些事情,比如添加
<DataTemplate x:Key="HeaderCheckbox">
<Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false"
Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}"
Visibility="Visible"
Width="200"/>
</DataTemplate>
在<DataGrid.Resources>
之后,在datagrid声明之前添加一个按钮,大多数时候,按钮确实没有出现。
非常感谢你的帮助 :)
只需在网格面板中再添加一行定义,然后只需将按钮添加到该特定行(网格上方的那一行),然后使用:
<Grid Name="Namegrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" CommandParameter="{Binding ElementName=NameDatagrid, Path=SelectedItems}"/>
<DataGrid Grid.Row="1" ItemsSource="{Binding ressource}" Name="NameDatagrid"/>
</Grid>
PS:请为您的DataGrid提供一个名称,并将其用于按钮内的ElementName。按钮应位于DataGrid控件本身上方,而不是内部。确保你也为DataGrid提到Grid.Row =“1”。