げんさん日記

プログラミングで気付いた事等を書きます。

DataGridのスクロールでイベントが勝手に発生する

2024年12月06日 10時57分40秒 | WPF
■現象 
DataGrid列のチェックボックスのUnCheckedイベントがスクロールするとイベント発生してしまう。
その為、Enabled指定をしたセルの状態が勝手に変わる。

●XAML
<DataGrid.Columns> 
    <DataGridTemplateColumn Header="使用" > 
        <DataGridTemplateColumn.CellTemplate> 
            <DataTemplate> 
                <CheckBox x:Name="SerialUseFlag" </div>
                          IsChecked="{Binding SerialUseFlagBool, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                          Style="{StaticResource NormalCheckStyle}" 
                          > 
                    <i:Interaction.Triggers> 
                        <i:EventTrigger EventName="Checked"> 
                            <i:InvokeCommandAction Command="{Binding DataContext.UseCheckCommand, </div">
                                                                     RelativeSource={RelativeSource FindAncestor,  
                                                                                                AncestorType={x:Type DataGrid}}}" 
                                                   CommandParameter="true" 
                                                   /> 
                        </i:EventTrigger> 
                        <i:EventTrigger EventName="Unchecked"> 
                            <i:InvokeCommandAction Command="{Binding DataContext.UseCheckCommand, </div">
                                                                     RelativeSource={RelativeSource FindAncestor,  
                                                                                                AncestorType={x:Type DataGrid}}}"  
                                                   CommandParameter="false" 
                                                   /> 
                        </i:EventTrigger> 
                    </i:Interaction.Triggers> 
                </CheckBox> 
            </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTemplateColumn Header="名称" > 
        <DataGridTemplateColumn.CellTemplate> 
            <DataTemplate> 
                <DockPanel HorizontalAlignment="Center" VerticalAlignment="Stretch"> 
                    <CheckBox IsChecked="{Binding HzSerialFlagBool," UpdateSourceTrigger="PropertyChanged," Mode="TwoWay}" </div>
                          IsEnabled="{Binding HzSerialFlagEnabled}" 
                          Style="{StaticResource NormalCheckStyle}" 
                          > 
                    </CheckBox> 
                </DockPanel> 
            </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid.Columns> 
使用チェックボックスの結果で名称チェックのEnableを切り替えているが、
スクロールするとEnableの設定が変わってしまう。 
 
■対応方法 
XamlでDataGridのScrollViewer.ScrollChangedをフックし、スクロールの場合はUncheckedイベントを無効にする。 
 
●Xaml
<DataGrid ItemsSource="{Binding SerialData}" </div>
          ScrollViewer.ScrollChanged="DataGrid_ScrollChanged" 
          >
ScrollViewer.ScrollChangedイベントを定義する。 
 
●コードビハインド 
       private void DataGrid_ScrollChanged(object sender, ScrollChangedEventArgs e) 
       { 
           // ViewModelのスクロールチェンジコマンドを実行する。 
           (this.DataContext as SerialPanelViewModel).SerialDataScrollChangedCommand.Execute(this); 
       } 
 
●ViewModel 
// チェックボックスイベント許可フラグ 
private bool IsAllowCheckBoxEvent { get; set; } = true;
  
// マウスダウン時、チェックボックスイベントの発生を許可する。 
private void OnSerialDataPreviewMouseDown() 
   this.IsAllowCheckBoxEvent = true; 
  
// スクロールチェンジ時、チェックボックスイベントの発生を不許可する。 
private void OnSerialDataScrollChanged() 
    this.IsAllowCheckBoxEvent = false; 
 
// チェック時、チェックボックスイベント許可フラグ=falseの時、処理を行わない。 
private void OnCheckUse(string mUseFlag) 
    if (this.IsAllowCheckBoxEvent == false) { return; } 
 
 


DataGridのCellTemplate内からViewModelにイベントを通知する。

2024年12月06日 08時10分54秒 | WPF
DataGridTemplateColumn内のコントロールからViewModelにコマンドを通知させます。


<DataGridTemplateColumn Header="使用" >
 <DataGridTemplateColumn.CellTemplate>
  <DataTemplate>
   <CheckBox x:Name="SerialUseFlag"
         IsChecked="{Binding ItemUseFlagBool}"
         Style="{StaticResource NormalCheckStyle}"
         >
    <i:Interaction.Triggers>
     <i:EventTrigger EventName="Checked">
      <i:InvokeCommandAction Command="{Binding DataContext.UseCheckCommand,
       RelativeSource={RelativeSource FindAncestor,
        AncestorType={x:Type DataGrid}}}"
       />
     </i:EventTrigger>
    </i:Interaction.Triggers>
   </CheckBox>
  </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

下のレベルからバインドする場合はRelativeSourceでコントロールに紐づいているDataContextから検索する必要があります。
この方法は他コントロールのテンプレートを作成した場合にも使用できます。






プロパティの型を判断する。

2022年07月06日 07時57分53秒 | C#
システム型
if (propertyInfo.PropertyType.Name == "Int32")

NULLL許可型
if (propertyInfo.PropertyType.IsGenericType == true &&       propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) 

ジェネリック配列型
if (propertyInfo.PropertyType.IsGenericType == true)

プロパティのジェネリック配列内の型を取得する。

2022年06月30日 10時41分57秒 | C#
プロパティに設定してあるジェネリック内の型を取得する。

var itemType = propertyInfo.PropertyType.GetGenericArguments().First(); 


プロパティの値を取得する。

2022年06月30日 10時02分20秒 | C#
システム型
var value = propertyInfo.GetValue(class); 

ジェネリック型
var values = (IEnumerable)propertyInfo.GetValue(class);