当我在数据网格中单击一个单元格时,它必须将单元格的值保存在变量中,以便我可以在下一页的标签中使用它。
这是我现在拥有的:
我的数据网格
<DataGrid x:Name="DataGridTilKonti" Grid.Row="1">
<DataGrid.Columns>
<DataGridTextColumn Header="Type"
HeaderStyle="{StaticResource DataGridHeaderStyle}"
Binding="{Binding kontoType}"/>
<DataGridTextColumn Header="Kontonummer"
HeaderStyle="{StaticResource DataGridHeaderStyle}"
Binding="{Binding kontoID}"/>
<DataGridTextColumn Header="Saldo"
HeaderStyle="{StaticResource DataGridHeaderStyle}"
Binding="{Binding saldo}"/>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseDown" Handler="DataGridCell_PreviewMouseDown"/>
</Style>
</DataGrid.CellStyle>
</DataGrid>
我的点击事件,只是我无数次尝试之一。
private void DataGridCell_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
//var grid = DataGridTilKonti as DataGrid;
//var cellValue = grid.SelectedValue;
//MainWindow.k.EnkeltKontoType = cellValue.ToString();
MainWindow.k.EnkeltKontoType =
DataGridTilKonti.SelectedCells[0].ToString();
NavigationService.Navigate(new EnkeltKontosTransaktioner());
}
我的DataGrid样式
<!-- Style til DataGrid -->
<Style TargetType="{x:Type DataGrid}">
<Setter Property="AutoGenerateColumns" Value="False"/>
<Setter Property="AlternatingRowBackground" Value="Gray"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="HeadersVisibility" Value="Column"/>
<Setter Property="CanUserSortColumns" Value="False"/>
<Setter Property="CanUserResizeColumns" Value="False"/>
<Setter Property="CanUserReorderColumns" Value="False"/>
<Setter Property="SelectionUnit" Value="Cell"/>
<Setter Property="SelectionMode" Value="Single"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0 20 0 0"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
<!-- Style til DataGridColumnHeaders -->
<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridHeaderStyle" BasedOn="{StaticResource BaseFontFamily}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Margin" Value="0 0 15 0"/>
</Style>
<!-- Der ligger en ekstra header under den som jeg ændrede på tidligere. Denne style gør den usynlig. -->
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Transparent"/>
</Style>
<!-- Style som fjerner den blå baggrundsfarve og sorte border når man trykker på en celle. -->
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
我当前收到的错误:
ArgumentOutOfRangeException:参数超出范围。
编辑1 :SelectionUnit
设置为Cell
,而SelectionMode
设置为Single
更新的错误输出。
答案 0 :(得分:0)
您可以在事件处理程序中获取DataGrid
的单元格值,如下所示。
private void DataGridCell_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell != null)
{
TextBlock tb = cell.Content as TextBlock;
MainWindow.k.EnkeltKontoType = tb.Text;
NavigationService.Navigate(new EnkeltKontosTransaktioner());
}
}