繁体   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?

这是我想要实现的图形想法,我将其称为 SuperControl:

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

插入的 UserControl 将在剩余的执行过程中保持不变。 这是我的方法

<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>

我创建了一个 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); }
        }
    }

因为我的想法是能够以这种方式定义 SuperControl:

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

但是我不知道在 InnerForm 属性中写什么来传递 UserControl 类并使此代码工作。

你有什么主意吗? 这甚至可能吗?

你快到了 - 我复制了你的代码,稍微调整了一下,得到了你想要的结果(如果那里的路径略有不同)。

SuperControl ,将您的ContentControl切换为ContentPresenter如下所示:

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

SuperControl声明(XAML 的顶级)还需要一个 name 属性,在我的示例中,我将其称为“superControl”。

无论您在哪里使用 SuperControl,您都可以使用 InnerForm 属性设置用户控件(已经设置了依赖项绑定等):

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

在我的示例中,我使用了 Rectangle,但您需要使用从 UserControl 继承的东西(因为我绑定到类型对象而不是 UserControl)。

我假设您没有 100% 承诺提供要注入的 XAML 文件的字符串路径,并且您只需要指定一个用户控件。 如果需要,此答案的好处是也可以在 XAML 中的用户控件上设置属性。

希望这可以帮助 :)

暂无
暂无

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

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