WPFアプリでは、グリッド内に多数のCustomControlsがあります。それらのマウスクリックを処理するためMouseLeftButtonDown
に、グリッドのイベントを使用し、イベントハンドラーで、どのCustomControlがクリックされたかを確認します。
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement feSourceComm = e.Source as FrameworkElement;
MyCustomControl SCurrentComm = new MyCustomControl();
try
{
SCurrentComm = (MyCustomControl)feSourceComm;
}
catch (Exception)
{
...
この問題は、すべてのCustomControlをUserControlに配置してから、グリッド内に配置したときに発生しました。この場合、アプローチは機能しません。
それぞれの場合にクリックソースのタイプを確認したところe.Source.GetType().ToString();
、次の結果が得られました。
問題がない場合(UserControlなしでCustomControlsをグリッドに配置した場合)
MyProjectNamespace.MyCustomControl
CustomControlsをUserControlに配置してからグリッドに配置すると
MyProjectNamespace.UserControls.MyUserControlName
CustomControlsをUserControlに配置し、次にグリッドに配置して、外部ファイルからUserControlをロードするとXamlReader.Load
System.Windows.Controls.UserControl
だから、私の質問:
CustomControlsをe.Source
UserControl内にあるときのように可視化するにはどうすればよいですか?