从主窗口WPF访问用户控件stackpanel

时间:2017-10-27 04:31:29

标签: c# wpf visual-studio

我有一个用户控件,其定义如下:

jQuery.extend({}, obj2, obj1);

现在我希望我的主窗口能够访问stackpanel,我该怎么做? 目前我的主窗口获取用户控件引用,如下所示:

<UserControl x:Class="LearnWPF1.DesignerPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<ScrollViewer VerticalScrollBarVisibility="Auto">
    <StackPanel     
              PreviewMouseDown="panel_PreviewMouseDown"          
              x:Name="panel"
              Orientation="Vertical" 
              Background="White"
              AllowDrop="True"
              DragOver="panel_DragOver"
              Drop="panel_Drop"/>
</ScrollViewer>

现在问题是PreviewMouseDown事件仅适用于UserControl的代码,如果我在MainWindow上执行此操作,它将不起作用。

2 个答案:

答案 0 :(得分:0)

-fstrict-aliasing中的

XAML

MainWindow
<local:DesignerPanel x:Name="DesignerPanel" PreviewMouseDown="DesignerPanel_OnPreviewMouseDown"/>

中的

Code-behind

MainWindow.cs

答案 1 :(得分:0)

您可以像:%s/^V^M//g 这样访问StackPanel

MainWindow