简体   繁体   English

我想通过 XAML 代码中的属性在其中插入另一个 UserControl 的项目和 contentControl 的用户控件如何?

[英]How have a UserControl with items and a contentControl in which I want to insert another UserControl by a property in XAML code?

This is a graphic idea of what I want to achieve I will call it SuperControl:这是我想要实现的图形想法,我将其称为 SuperControl:

---------------------------------------------  
|                                           |  
|   ----------   ------------------------   |  
|   | Button |   |  TextBox             |   |  
|   ----------   ------------------------   |  
|                                           |  
|    ------------------------------------   |  
|    |                                  |   |  
|    |    Another UserControl that      |   |  
|    |    can be different depending    |   |  
|    |    on the problem to be solved   |   |  
|    |                                  |   |
|    ------------------------------------   |
|                                           |
---------------------------------------------

The UserControl inserted will remain constant for the rest of the execution.插入的 UserControl 将在剩余的执行过程中保持不变。 This is my aproach这是我的方法

<UserControl x:Class="App.GUI.Items.SuperControl">
     <Grid>
         <!-- Some row and column definitions -->
         <Button Content="Button" Click="Btn_Click" Grid.Column="0" Grid.Row="0"/>
         <TextBox Text="textBox" Grid.Column="1" Grid.Row="0"/>
         <ContentControl x:Name="innerForm" Grid.Row="0" Grid.ColumnSpan="2"/>
     </Grid>
</UserControl>

And I created a DependencyProperty我创建了一个 DependencyProperty

   public partial class SuperControl : UserControl
    {
        public SuperControl()
        {
            InitializeComponent();
            //I know that I have declare a new Userform(), but I do not know how
            innerForm = this.InnerForm; 
        }

        public static readonly DependencyProperty InnerFormProperty =
            DependencyProperty.Register("InnerForm", typeof(UserControl), typeof(SuperControl));

        public UserControl InnerForm
        {
            get { return (UserControl)GetValue(InnerFormProperty); }
            set { SetValue(InnerFormProperty, value); }
        }
    }

Because my idea is to be able to define the SuperControl this way:因为我的想法是能够以这种方式定义 SuperControl:

<Items:SuperControl InnerForm="OneOfTheSeveralUserControlsIMightWantToInsert.xaml"/>

But I do not know what to write in the InnerForm property to pass a UserControl class and make this code work.但是我不知道在 InnerForm 属性中写什么来传递 UserControl 类并使此代码工作。

Do you have any idea?你有什么主意吗? Is this even posible?这甚至可能吗?

You're nearly there - I copied your code, tweaked it a bit and got the result you wanted (if a slightly different path there).你快到了 - 我复制了你的代码,稍微调整了一下,得到了你想要的结果(如果那里的路径略有不同)。

In the SuperControl , switch your ContentControl for a ContentPresenter like so:SuperControl ,将您的ContentControl切换为ContentPresenter如下所示:

    <ContentPresenter Margin="58,118,318,52" Content="{Binding InjectedContent, ElementName=superControl}">
    </ContentPresenter>

The SuperControl declaration (top level of XAML) will also need a name property, in my example I called it "superControl". SuperControl声明(XAML 的顶级)还需要一个 name 属性,在我的示例中,我将其称为“superControl”。

Wherever you're using the SuperControl, you can set a user control using the InnerForm property (already set up with dependency binding etc):无论您在哪里使用 SuperControl,您都可以使用 InnerForm 属性设置用户控件(已经设置了依赖项绑定等):

    <local:SuperControl>
        <local:SuperControl.InjectedContent>
            <Rectangle Fill="Red" />
        </local:SuperControl.InjectedContent>
    </local:SuperControl>

In my example I've used a Rectangle but you will need to use something that inherits from UserControl (since I was binding to type object rather than a UserControl).在我的示例中,我使用了 Rectangle,但您需要使用从 UserControl 继承的东西(因为我绑定到类型对象而不是 UserControl)。

I made the assumption that you weren't 100% committed to providing a string path to a XAML file to inject, and that you just needed to specify a user control.我假设您没有 100% 承诺提供要注入的 XAML 文件的字符串路径,并且您只需要指定一个用户控件。 This answer has the benefit of being able to set properties on the user control in XAML too, if required.如果需要,此答案的好处是也可以在 XAML 中的用户控件上设置属性。

Hope this helps :)希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM