What i want to do is to add event handlers to the controls inside canvasBackground so i could move all its child elements to different window location using mouse dragging. But when it starts looping through each Controls in foreach loop, it throws an exception:
XamlParseException occurred The invocation of the constructor on type " AgentWindow.xaml " that matches the specified binding constraints threw an exception. Line number '3' and line position '9'.
Source information is missing from the debug information for this module
AgentWindow.xaml.cs :
public AgentWindow()
{
InitializeComponent();
foreach (Control control in canvasBackground.Children)
{
control.PreviewMouseLeftButtonDown += this.MouseLeftButtonDown;
control.PreviewMouseMove += this.MouseMove;
control.PreviewMouseLeftButtonUp += this.PreviewMouseLeftButtonUp;
}
}
AgentWindow.xaml :
<Canvas x:Name="canvasBackground">
<Canvas Background="Black" Canvas.Right="10" Canvas.Top="10" Height="200" Width="153">
<Button x:Name="btnF1" Content="f1" Height="56" Width="60" Margin="10,12,79,132" Background="#DDD"></Button>
<Button x:Name="btnF2" Content="f2" Height="56" Width="60" Margin="80,12,9,132" Background="#DDD"/>
<Button x:Name="btnF3" Content="f3" Height="56" Width="60" Margin="10,73,79,71" Background="#DDD"></Button>
<Button x:Name="btnF4" Content="f4" Height="56" Width="60" Margin="80,73,9,71" Background="#DDD"></Button>
<Button x:Name="btnF5" Content="f5" Height="56" Width="60" Margin="10,134,79,10" Background="#DDD"></Button>
<Button x:Name="btnF6" Content="f6" Height="56" Width="60" Margin="80,134,9,10" Background="#DDD"></Button>
</Canvas>
<Button x:Name="btnSomeButton" Content="someb" Height="56" Width="60"
Background="#FFFBFBFB" Canvas.Left="292" Canvas.Top="193"/>
</Canvas>
All this started when i changed GroupBox to Canvas. I did that because i couldn't figure out how to access those buttons inside GroupBox... I'm new to wpf so please be gentle. :)
Canvas
is not a Control
. Also Children is UIElementCollection
so you should loop over UIElement
and not Control.
foreach (UIElement control in canvasBackground.Children)
{
....
}
On a side note this loop will hook mouse events on child Canvas and button, not on inner buttons. If you want to do for inner buttons, you have to loop over child of inner canvas as well.
Anyhow, you can use XAML only to hook events. Hook on parent element itself.
<Canvas x:Name="canvasBackground"
PreviewMouseLeftButtonDown="MouseLeftButtonDown"
PreviewMouseMove="MouseMove"
PreviewMouseLeftButtonUp="PreviewMouseLeftButtonUp">
<Canvas>
<Button x:Name="btnF1"/>
<Button x:Name="btnF2"/>
<Button x:Name="btnF3"/>
<Button x:Name="btnF4"/>
<Button x:Name="btnF5"/>
<Button x:Name="btnF6"/>
</Canvas>
<Button x:Name="btnSomeButton" Content="someb" Height="56" Width="60"
Background="#FFFBFBFB" Canvas.Left="292" Canvas.Top="193"/>
</Canvas>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.