2 つのカスタム userControls があります。そして、いくつかのプロパティを customUserControl に設定したい場合は、次のようにする必要があります。
private void OnRightMouseDown(object sender, MouseButtonEventArgs e)
{
var userControl = sender as UserControl;
if (userControl != null)
switch (userControl.Name)
{
case "UserControl01":
var uc01 = sender as UserControl01;
if (uc01 != null)
{
uc01.ViewModel.IsSelected = true;
}
break;
case "UserControl02":
var uc02 = sender as UserControl02;
if (uc02 != null)
{
uc02.ViewModel.IsSelected = true;
}
break;
}
e.Handled = true;
}
そして、私はこのようにしたい:
private void OnRightMouseDown(object sender, MouseButtonEventArgs e)
{
var userControl = sender as UserControl;
if (userControl != null)
{
var tempUc = GetUserControlType(userControl);
tempUc.ViewModel.IsSelected = true;
}
e.Handled = true;
}
その目的のために、私はGetUserControlType
メソッドを作りました:
private static T GetUserControlType<T>(T userControl)
{
if (userControl != null)
{
var uc = userControl as UserControl;
switch (uc.Name)
{
case "UserControl01":
var tempUc1 = userControl as UserControl01;
return tempUc1;
case "UserControl02":
var tempUc2 = userControl as UserControl02;
return tempUc2;
}
}
return default(T);
}
そして、エラーが発生します-Cannot convert expression type '' to return type 'T' in line return tempUc1;
この2つのタイプのいずれかを返す必要があるため、どうすれば回避できますか?