Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调试信息中缺少源信息_C#_Wpf_Xaml_Exception_Foreach - Fatal编程技术网

C# 调试信息中缺少源信息

C# 调试信息中缺少源信息,c#,wpf,xaml,exception,foreach,C#,Wpf,Xaml,Exception,Foreach,我想做的是将事件处理程序添加到canvasBackground中的控件,以便使用鼠标拖动将其所有子元素移动到不同的窗口位置。 但当它开始在foreach循环中的每个控件之间循环时,它会抛出一个异常: 调用AgentWindow.xaml类型上的构造函数时发生XamlParseException 与指定绑定约束匹配的引发了异常。行号“3”和行位置“9” 此模块的调试信息中缺少源信息 AgentWindow.xaml.cs: AgentWindow.xaml: 这一切都是从我将GroupBox更改为

我想做的是将事件处理程序添加到canvasBackground中的控件,以便使用鼠标拖动将其所有子元素移动到不同的窗口位置。 但当它开始在foreach循环中的每个控件之间循环时,它会抛出一个异常:

调用AgentWindow.xaml类型上的构造函数时发生XamlParseException 与指定绑定约束匹配的引发了异常。行号“3”和行位置“9”

此模块的调试信息中缺少源信息

AgentWindow.xaml.cs:

AgentWindow.xaml:

这一切都是从我将GroupBox更改为Canvas开始的。我这样做是因为我不知道如何访问GroupBox中的按钮。。。我是wpf的新手,所以请温柔一点

画布不是控件。子元素也是UIElementCollection,所以您应该在UIElement上循环,而不是控制

foreach (UIElement control in canvasBackground.Children)
{
   ....
}
另一方面,这个循环将鼠标事件挂在子画布和按钮上,而不是挂在内部按钮上。若要对内部按钮执行此操作,还必须在内部画布的子级上循环

无论如何,您只能使用XAML来钩住事件。钩住父元素本身

<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>

啊。。这太容易了。。非常感谢;
foreach (UIElement control in canvasBackground.Children)
{
   ....
}
<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>