gpt4 book ai didi

c# - ControlTemplate 中的边框导致 DataGrid 出现奇怪的选择行为

转载 作者:行者123 更新时间:2023-11-30 22:49:36 25 4
gpt4 key购买 nike

我已经将 Microsoft WPF DataGrid 中的 DataGridRow 重新模板化到下面,我遇到的问题是如果用户单击模板的边框元素,则不会选择行。有没有办法让点击边框导致行选择。

<Grid x:Name="LayoutRoot" Margin="0,0,0,-1">
<Border x:Name="DGR_Border" BorderBrush="Transparent" Background="Transparent" BorderThickness="1" CornerRadius="5" SnapsToDevicePixels="True">
<Border x:Name="DGR_InnerBorder" BorderBrush="Transparent" Background="Transparent" BorderThickness="1" CornerRadius="5" SnapsToDevicePixels="True">
<toolkit:SelectiveScrollingGrid Name="DGR_SelectiveScrollingGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<toolkit:DataGridCellsPresenter Grid.Column="1" Name="DGR_CellsPresenter" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<toolkit:DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" Visibility="{TemplateBinding DetailsVisibility}" toolkit:SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:DataGrid}}, Path=AreRowDetailsFrozen, Converter={x:Static Controls:DataGrid.RowDetailsScrollingConverter}, ConverterParameter={x:Static Controls:SelectiveScrollingOrientation.Vertical}}" />
<toolkit:DataGridRowHeader Grid.RowSpan="2" toolkit:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:DataGrid}}, Path=HeadersVisibility, Converter={x:Static Controls:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static Controls:DataGridHeadersVisibility.Row}}"/>
</toolkit:SelectiveScrollingGrid>
</Border>
</Border>
</Grid>

最佳答案

调用内部方法似乎有些危险。如果实现细节发生变化怎么办?以前的版本有很多变化。

我认为简单地将这样的事件处理程序添加到您的行中可能更为谨慎:

protected void DataGridRow_MouseDown(object sender, MouseButtonEventArgs e)
{
// GetVisualChild<T> helper method, simple to implement
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

// try to get the first cell in a row
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0);
if (cell != null)
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(MouseLeftButtonDownEvent);
//if the DataGridSelectionUnit is set to FullRow this will have the desired effect
cell.RaiseEvent(newEventArgs);
}
}

这与单击单元格本身具有相同的效果,并且将仅使用 DataGrid 元素的公共(public)成员。

关于c# - ControlTemplate 中的边框导致 DataGrid 出现奇怪的选择行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1069285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com