[英]How to find value of Datagrid row that I have pressed 'DELETE' key on using WPF
[英]How do I have a floating delete row button for a DataGrid in WPF?
我正在嘗試對我正在構建的DataGrid
進行最后潤色,而我在獲取我正在尋找的結果時遇到了一些麻煩。 我有一個包含我的行的DataGrid
,我需要能夠讓用戶從網格中刪除選定的列。 最初,我通過一個從網格外部的按鈕來做到這一點。 一位同事建議我把按鈕放在網格內,所以我做了。 該按鈕僅顯示所選行。 這是XAML:
<DataGrid MaxHeight="105" DockPanel.Dock="Left"
ItemsSource="{Binding Item}"
SelectedItem="{Binding SelectedItem}"
AutoGenerateColumns="False" HorizontalScrollBarVisibility="Disabled">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Part Number" Binding="{Binding PartNumber}" IsReadOnly="False"/>
<DataGridTextColumn Width="*" Header="Date Code" Binding="{Binding DateCode}" IsReadOnly="False"/>
<DataGridTextColumn Width="*" Header="Lot Code" Binding="{Binding LotCode}" IsReadOnly="False"/>
<DataGridTemplateColumn Header="" Width="Auto" CellStyle="{StaticResource NoBorderCellStyle}" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="X" HorizontalAlignment="Center"
Command="{Binding Path=DataContext.DeleteItem,RelativeSource={RelativeSource AncestorType={x:Type DataGrid},Mode=FindAncestor}}" Padding="5,0">
<Button.Style>
<Style>
<Setter Property="Button.Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=IsSelected}" Value="True">
<Setter Property="Button.Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
這很好用,但我不喜歡最后掛出的DataGrid
的最后一列。 我試過它的樣式,以便最后一列只是透明的,但到目前為止還沒有這樣的運氣。
我認為這里的理想解決方案是讓按鈕位於DataGrid
外部,但是要跟隨選定的行。 我不知道該怎么做。 我可能不得不將按鈕的邊距綁定到DataGrid
,但我不確定如何獲取所選行或計算正確的邊距。 不幸的是,我沒有谷歌果汁找到任何關於此的信息。
任何有關這方面的幫助將不勝感激。
謝謝。
編輯添加:
我發布了一個解決方案 如果您有更好的解決方案,請發布,如果我同意它更好,我會將其標記為已回答。
我找到了解決方案。 我對代碼的布局並不感到興奮。 我使用了幾行感覺可能需要清理的糟糕黑客,但這確實有效。 通常,我試圖遠離UI的代碼,但這是所有布局的東西,所以我會給它一個通行證。
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
refreshButton();
}
private void dataGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
refreshButton();
}
private void refreshButton()
{
if (dataGrid.SelectedItem != null)
{
Int32 val;
// This is the first bit that I don't like. I pulled this code from
// another website that showed how to get the selected row as a FrameworkElement
// There is perhaps a more intuitive way to do this
FrameworkElement element = dataGrid.Columns.Last().GetCellContent(dataGrid.SelectedItem);
Point selectedRow = element.TransformToAncestor(this).Transform(new Point(0, 0));
Point grd = dataGrid.TransformToAncestor(this).Transform(new Point(0, 0));
val = Int32.Parse(Math.Round(selectedRow.Y + element.ActualHeight/2 - grd.Y - DelButton.ActualHeight/2).ToString());
// This is the other bit I don't like. Magic numbers to make sure the delete
// box doesn't hang out above or below on a scroll.
// I think I can get around this by getting the bounds of the elements and
// doing the math off of that. I'll probably swing back to it later.
if (val > 110 || val < 10)
DelButton.Visibility = System.Windows.Visibility.Hidden;
else
DelButton.Visibility = System.Windows.Visibility.Visible;
Canvas.SetTop(DelButton, val);
}
else
DelButton.Visibility = System.Windows.Visibility.Hidden;
}
所以這里的基本思想是我有一個Canvas,它在我的DataGrid旁邊,里面有一個按鈕。 畫布的高度可調,以便在DataGrid增長時與其對齊。 當選擇更改或DataGrid滾動時,我們更新按鈕的位置以及它是否可見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.