2009-06-09 57 views
6

我找到了这个问题的ASP,但它并没有帮助我很多......可以包含其他控件(使用时)C#用户控制

我希望做的是以下:我想创建一个具有集合作为属性的集合的用户控件,以及用于导航该集合的按钮。我希望能够将此用户控件绑定到集合并在其上显示不同的控件(包含来自该集合的数据)。 就像你在一个形式下缘的MS Access有什么...

更精确:

当我实际使用我的应用程序的控制(后我创造了它),我想能够在<myControly></mycontrol>之间添加多个控件(文本框,标签等) 如果我现在这样做了,那么我的用户控件上的控件就会消失。

+2

接受的答案什么问题呢?这听起来很容易...... – Tony 2009-06-09 12:39:23

+0

问题不在于收集和导航按钮,而在于我希望在另一个应用程序中使用它时,可以将控件添加到用户控件中。 – 2009-06-09 12:52:28

+0

如果我尝试在和之间放置多个控件,那么我只能在其中放置一个控件。如果我尝试一个网格,我的自定义控件上的控件就会消失。 – 2009-06-09 13:20:05

回答

8

这里是做你想要什么的一种方式的示例:

首先,代码 - UserControl1.xaml.cs

public partial class UserControl1 : UserControl 
{ 
    public static readonly DependencyProperty MyContentProperty = 
     DependencyProperty.Register("MyContent", typeof(object), typeof(UserControl1)); 


    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    public object MyContent 
    { 
     get { return GetValue(MyContentProperty); } 
     set { SetValue(MyContentProperty, value); } 
    } 
} 

而且用户控件的XAML - UserControl1.xaml

<UserControl x:Class="InCtrl.UserControl1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300" Name="MyCtrl"> 
    <StackPanel> 
     <Button Content="Up"/> 
     <ContentPresenter Content="{Binding ElementName=MyCtrl, Path=MyContent}"/> 
     <Button Content="Down"/> 
    </StackPanel> 
</UserControl> 

最后,XAML使用我们美好的新控制:

<Window x:Class="InCtrl.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:me="clr-namespace:InCtrl" 
    Title="Window1" Height="300" Width="300"> 
    <Grid> 
     <me:UserControl1> 
      <me:UserControl1.MyContent> 
       <Button Content="Middle"/> 
      </me:UserControl1.MyContent> 
     </me:UserControl1> 
    </Grid> 
</Window> 
0

UserControl可能不是实现此目的的最佳方法。你想在内容上添加装饰,这基本上是Border所做的:它有一个子元素,并在边缘添加自己的东西。

查看Decorator类,Border是从哪里下来的。如果你制作自己的边境后裔,你应该很容易做到你想要的。不过,我相信这需要编写代码,而不是XAML。

您可能仍然想要使用UserControl来将按钮封装在底部,这样您就可以将可视化设计器用于部分流程。但装饰者将是将各个部分粘合在一起并允许用户定义的内容的好方法。

0

这里有一个link到内置的控制(HeaderedContentControl),做同样的事情,不同之处在于它是在WPF现有的控制,因为NET 3.0

相关问题