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