可以包含其他控件的C#用户控件(使用时)

前端之家收集整理的这篇文章主要介绍了可以包含其他控件的C#用户控件(使用时)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在ASP中发现了一些关于这个问题的东西,但它对我帮助不大……

我想做的是以下内容:我想创建一个用户控件,它具有一个集合作为属性和按钮来浏览此集合.我希望能够将此用户控件绑定到一个集合并在其上显示不同的控件(包含该集合中的数据).
就像你在表格下边缘的MS Access中所拥有的一样……

更确切地说:

当我在我的应用程序中实际使用该控件时(在我创建它之后),我希望能够在< myControly>之间添加多个控件(文本框,标签等).和< / mycontrol>
如果我现在这样做,我的用户控件上的控件就会消失.

@R_403_323@

以下是一种实现您想要的方法的示例:

首先,代码 – 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>
原文链接:https://www.f2er.com/csharp/244376.html

猜你在找的C#相关文章