Okay, I have a bit of a weird bug...
This works fine:
private void radioButtonNormalPoint_Checked(object sender, RoutedEventArgs e)
{
//comboBoxNormalPoint.SelectedIndex = 0;
//ellipsePoint.Fill = System.Windows.Media.Brushes.Black;
}
This throws System.Reflection.TargetInvocationException
:
private void radioButtonNormalPoint_Checked(object sender, RoutedEventArgs e)
{
comboBoxNormalPoint.SelectedIndex = 0;
ellipsePoint.Fill = System.Windows.Media.Brushes.Black;
}
Also, it doesn't let me debug it; it crashes as the program loads. If I put a breakpoint anywhere it doesn't hit it; it just errors right away.
I think you will have fewer problems if you declared a Property that implements INotifyPropertyChanged, then databind IsChecked
, SelectedIndex
(using IValueConverter) and Fill
(using IValueConverter) to it instead of using the Checked Event to toggle SelectedIndex
and Fill
.
This is often caused by an attempt to process a null object. An example, trying to empty a Bindable list that is null will trigger the exception:
public class MyViewModel {
[BindableProperty]
public virtual IList<Products> ProductsList{ get; set; }
public MyViewModel ()
{
ProductsList.Clear(); // here is the problem
}
}
This could easily be fixed by checking for null:
if (ProductsList!= null) ProductsList.Clear();
If the radiobutton-checked event occurs before the content of the window is loaded fully, i.e. the ellipse is loaded fully, such an exception will be thrown. So check if the UI of the window is loaded (probably by Window_ContentRendered event, etc.).
To diagnose this issue, place the line of code causing the TargetInvocationException inside the try block.
To troubleshoot this type of error, get the inner exception. It could be due to a number of different issues.
try
{
// code causing TargetInvocationException
}
catch (Exception e)
{
if (e.InnerException != null)
{
string err = e.InnerException.Message;
}
}
Source: Stackoverflow.com