如何让用户控件返回其项目?

时间:2013-11-17 01:10:07

标签: c# wpf xaml canvas

我有一个带有画布的用户控件。我希望能够引用canvas元素,但我不能。

我试图在代码隐藏中创建一个方法来返回画布,但它不起作用,因为当我需要使用它时,我必须创建一个用户控件的新实例,它将包含一个空画布。我需要当前显示窗口中的画布。

这是我创建的用于返回画布的方法:

  public DesignerCanvas ret()
    {
        return this.MyDesigner;
    }

这是XAML:

  <UserControl x:Class="DiagramDesigner.WindowsUserControl"
                 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" 
                 xmlns:s="clr-namespace:DiagramDesigner"
                 xmlns:usercontrols="clr-namespace:DiagramDesigner"
            xmlns:c="clr-namespace:DiagramDesigner.Controls"
                 mc:Ignorable="d" 

                 d:DesignHeight="700" d:DesignWidth="1000">
        <UserControl.Resources>
            <ContextMenu x:Key="DesignerCanvasContextMenu" >
                <MenuItem Header="Paste" Command="{x:Static ApplicationCommands.Paste}">
                    <MenuItem.Icon>
                        <Image Source="Resources/Images/Paste.png" Width="16"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Select All" Command="{x:Static s:DesignerCanvas.SelectAll}"/>
            </ContextMenu>
        </UserControl.Resources>

        <Grid Margin="10">
            <Grid.RowDefinitions>

                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>


            <Grid Grid.Row="1" Margin="0,10,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <!-- Toolbox -->
                <StackPanel Grid.Column="0" Margin="0,0,5,0" >
                    <usercontrols:UserControl1></usercontrols:UserControl1>
                </StackPanel>
                <!-- GridSplitter -->
                <GridSplitter Focusable="False" Width="2" Background="LightGray"
                        VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
                <!-- Designer -->
                <GroupBox Header="Diagram" Grid.Column="1" Margin="3,0,0,0">
                    <ScrollViewer HorizontalScrollBarVisibility="Auto"
                          VerticalScrollBarVisibility="Auto">
                        <s:DesignerCanvas Focusable="true" x:Name="MyDesigner"
                                Background="{StaticResource WindowBackgroundBrush}"
                                Margin="10" FocusVisualStyle="{x:Null}"
                                ContextMenu="{StaticResource DesignerCanvasContextMenu}"/>
                    </ScrollViewer>
                </GroupBox>
            </Grid>
        </Grid>

    </UserControl>

这是我的usercontrol.xaml.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DiagramDesigner
{
    /// <summary>
    /// Interaction logic for WindowsUserControl.xaml
    /// </summary>
    public partial class WindowsUserControl : UserControl
    {
        public WindowsUserControl()
        {
            InitializeComponent();

        }
        public DesignerCanvas ret()
        {
            return this.MyDesigner;
        }
    }
}

mainwindow.xaml代码

<Window x:Class="DiagramDesigner.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:DiagramDesigner"
        xmlns:c="clr-namespace:DiagramDesigner.Controls"
        xmlns:usercontrols="clr-namespace:DiagramDesigner"

          WindowStartupLocation="CenterScreen" WindowState="Maximized"
        Title="GEN Diagram Designer"
        Height="700" Width="1000" Icon="Resources/Images/coollogo_com-61024358.png">

    <Window.Resources>
        <ContextMenu x:Key="DesignerCanvasContextMenu">
            <MenuItem Header="Paste" Command="{x:Static ApplicationCommands.Paste}">
                <MenuItem.Icon>
                    <Image Source="Resources/Images/Paste.png" Width="16"/>
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Select All" Command="{x:Static s:DesignerCanvas.SelectAll}"/>
        </ContextMenu>
    </Window.Resources>

    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ContentControl Content="{StaticResource MyToolbar}"/>
        <Button Margin="776.346,40.022,75.653,39.977" Click="Button_Click_1" Width="120" Height="40" >Generate Code</Button>

        <Grid Grid.Row="1">
            <usercontrols:WindowsUserControl Loaded="WindowsUserControl_Loaded">

            </usercontrols:WindowsUserControl>

        </Grid>
    </Grid>
</Window>

我的mainwindow.xaml.cs代码

  using System.Windows;
    using System.Collections.Generic;
    using System.Xml;
    using System.Linq;
    namespace DiagramDesigner
    {
        public partial class Window1 : Window 
        {
            public Window1()
            {
                InitializeComponent();
            }

            private void Button_Click_1(object sender, RoutedEventArgs e)
            {


                XmlDocument doc = new XmlDocument();
                XmlElement elem = doc.CreateElement("code");
                doc.AppendChild(elem);

                List<DesignerItem> L = new List<DesignerItem>();

                foreach (DesignerItem d in ***)
                {
                    L.Add(d);
                }

                var orderedItems = L.OrderBy(item => DesignerCanvas.GetTop(item)).ToList();

                foreach (DesignerItem d in orderedItems)
                {
                    XmlNode ChildNode = doc.ImportNode(d.s.code(), true);
                    doc.FirstChild.AppendChild(ChildNode);

                }

                doc.Save(@"D:\code.xml");
            }

            private void WindowsUserControl_Loaded(object sender, RoutedEventArgs e)
            {

            }

        }
    }
在mainwindow.xaml.cs中 每个 * 的第一个,我想要获得我的画布

1 个答案:

答案 0 :(得分:2)

为您的控件命名:

<usercontrols:WindowsUserControl Loaded="WindowsUserControl_Loaded" x:Name="someName" />

然后使用该名称引用控件:

private void WindowsUserControl_Loaded(object sender, RoutedEventArgs e)
{
    var theCanvasFromTheUserControl = someName.ret();

    // now do something with theCanvasFromTheUserControl
}