开发者

WPF application won't execute when i add simple event

开发者 https://www.devze.com 2022-12-13 19:01 出处:网络
I have a window, in which i have many controls,UserControls or controls derived from other controls (and grids and frames).

I have a window, in which i have many controls, UserControls or controls derived from other controls (and grids and frames).

It works fine if i DON'T add any event to ANY of the controls i have in my window (in XAML). I have many other events that don't cause this, but if i add a new event.开发者_C百科 It will crash.

Example:

This is the control i would like to add my event to:

<con:MyControl Content="Hello" Grid.Column="3" Width="90"/>

So, i change it into:

<con:MyControl Content="Hello" Grid.Column="3" Width="90" Click="Hello_Click"/>

The application compiles...

But then, this happens:

'Set connectionId threw an exception.' Line number '53' and line position '22'.

InnerException:

{"Unable to cast object of type 'System.Windows.Controls.Button' to type 'MyNamespace.MyClass.MyControl'."}

If i now remove

Click="Hello_Click"

It works perfectly fine!


I agree with Anurag. If you have a button elsewhere on your client that is hosting MyControl you may be able to attach the button event to your control by changing Click="Hello_Click" to Button.Click="Hello_Click". But you would need to have a button further down the logical hierachy so its event could bubble up to your control. The error suggests that MyControl does not subclass Button.

0

精彩评论

暂无评论...
验证码 换一张
取 消