C# 如何使用户控件返回其项?
我有一个带有画布的用户控件。我希望能够引用canvas元素,但我不能 我试图在代码隐藏中创建一个方法来返回画布,但它不起作用,因为当我需要使用它时,我必须创建一个新的用户控件实例,该实例将包含一个空画布。我需要当前显示窗口中的画布 下面是我创建的返回画布的方法:C# 如何使用户控件返回其项?,c#,wpf,xaml,canvas,C#,Wpf,Xaml,Canvas,我有一个带有画布的用户控件。我希望能够引用canvas元素,但我不能 我试图在代码隐藏中创建一个方法来返回画布,但它不起作用,因为当我需要使用它时,我必须创建一个新的用户控件实例,该实例将包含一个空画布。我需要当前显示窗口中的画布 下面是我创建的返回画布的方法: public DesignerCanvas ret() { return this.MyDesigner; } 以下是XAML: <UserControl x:Class="Diagram
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间图设计器
{
///
///WindowsUserControl.xaml的交互逻辑
///
公共部分类WindowsUserControl:UserControl
{
公共WindowsUserControl()
{
初始化组件();
}
公共设计师Canvas ret()
{
返回此.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)
{
}
}
}
使用System.Windows;
使用System.Collections.Generic;
使用System.Xml;
使用System.Linq;
命名空间图设计器
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
XmlDocument doc=新的XmlDocument();
XmlElement elem=doc.CreateElement(“代码”);
附录子文件(elem);
列表L=新列表();
foreach(设计人员姓名***)
{
L.加入(d);
}
var orderedItems=L.OrderBy(item=>DesignerCanvas.GetTop(item)).ToList();
foreach(orderedItems中的DesignerItem d)
{
XmlNode ChildNode=doc.ImportNode(d.s.code(),true);
doc.FirstChild.AppendChild(ChildNode);
}
doc.Save(@“D:\code.xml”);
}
已加载私有void WindowsUserControl_(对象发送方,路由目标)
{
}
}
}
在mainwindow.xaml.cs中
每个*的第一个*在这里,我要获取画布为控件命名:
<usercontrols:WindowsUserControl Loaded="WindowsUserControl_Loaded" x:Name="someName" />
我为我的错误文本感到抱歉,我要让它变得更好thx for advancei我有使用此用户控件的窗口的实例我的问题是如何获取用户控件及其元素?我发布了完整的代码和xaml,以及我想在哪里获取画布(用户控件项)我在我的主窗口外定义了我的用户控件,我在许多窗口中使用它,其中一个是主窗口,如果它不起作用,我将尝试这个方法,我将发布代码,xamlthx Aloot现在就批准它的工作,我得到了我应该为同一领域的未来问题做的事情:)
private void WindowsUserControl_Loaded(object sender, RoutedEventArgs e)
{
var theCanvasFromTheUserControl = someName.ret();
// now do something with theCanvasFromTheUserControl
}