行の選択に使用できるオプションがいくつかありますが、「選択なし」はその1つではありません。SelectedItemをnullに設定してSelectionChangedイベントを処理しようとしましたが、行がまだ選択されているようです。
これを防ぐための簡単なサポートがない場合、選択した行を選択していない行と同じようにスタイル設定するのは簡単でしょうか?そうすれば選択できますが、ユーザーには視覚的なインジケーターがありません。
行の選択に使用できるオプションがいくつかありますが、「選択なし」はその1つではありません。SelectedItemをnullに設定してSelectionChangedイベントを処理しようとしましたが、行がまだ選択されているようです。
これを防ぐための簡単なサポートがない場合、選択した行を選択していない行と同じようにスタイル設定するのは簡単でしょうか?そうすれば選択できますが、ユーザーには視覚的なインジケーターがありません。
動作させるには、BeginInvokeと非同期でDataGrid.UnselectAllを呼び出す必要があります。これを処理するために、次の添付プロパティを作成しました。
using System;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Windows.Controls;
namespace DataGridNoSelect
{
public static class DataGridAttach
{
public static readonly DependencyProperty IsSelectionEnabledProperty = DependencyProperty.RegisterAttached(
"IsSelectionEnabled", typeof(bool), typeof(DataGridAttach),
new FrameworkPropertyMetadata(true, IsSelectionEnabledChanged));
private static void IsSelectionEnabledChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var grid = (DataGrid) sender;
if ((bool) e.NewValue)
grid.SelectionChanged -= GridSelectionChanged;
else
grid.SelectionChanged += GridSelectionChanged;
}
static void GridSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var grid = (DataGrid) sender;
grid.Dispatcher.BeginInvoke(
new Action(() =>
{
grid.SelectionChanged -= GridSelectionChanged;
grid.UnselectAll();
grid.SelectionChanged += GridSelectionChanged;
}),
DispatcherPriority.Normal, null);
}
public static void SetIsSelectionEnabled(DataGrid element, bool value)
{
element.SetValue(IsSelectionEnabledProperty, value);
}
public static bool GetIsSelectionEnabled(DataGrid element)
{
return (bool)element.GetValue(IsSelectionEnabledProperty);
}
}
}
私は自分のソリューションを作成する際にこのブログ投稿を入手しました。
問題を解決するには、以下のスタイルをdatagidセルに適用してください。
<Style x:Key="MyDatagridCellStyle" TargetType="{x:Type Custom:DataGridCell}">
<Setter Property="Focusable" Value="false"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="#434342"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="11"/>
<Setter Property="FontWeight" Value="Normal"/>
</Style>
プロパティ'IsHitTestVisible'をFalseとして使用すると、行の選択を回避できます。ただし、データグリッドのスクロールバーを使用することはできません。この場合、データグリッドはロックされます。別の解決策は次のとおりです。データグリッドのセルにスタイルを適用できます。それは私のために働いた。以下のコードを使用してください:
上記のコードは私のために働いた。それがあなたにもうまくいくことを願っています。
よろしく、ヴァイシャリ