【发布时间】:2014-04-20 08:15:43
【问题描述】:
我想要做的是将事件处理程序添加到 canvasBackground 内的控件中,这样我就可以使用鼠标拖动将其所有子元素移动到不同的窗口位置。 但是当它开始循环遍历 foreach 循环中的每个 Controls 时,它会抛出异常:
发生 XamlParseException 对“AgentWindow.xaml”类型的构造函数的调用 与指定的绑定约束匹配的会引发异常。行号“3”和行位置“9”。
此模块的调试信息中缺少源信息
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>
这一切都是在我将 GroupBox 更改为 Canvas 时开始的。我这样做是因为我不知道如何访问 GroupBox 中的那些按钮......我是 wpf 的新手,所以请温柔一点。 :)
【问题讨论】:
标签: c# wpf xaml exception foreach