How to add controls dynamically to a UserControl through user's XAML?

后端 未结 1 1564
暗喜
暗喜 2021-01-18 21:09

I want to create a user control that contains a TextBlock and a StackPanel that will allow the user to add his/her own controls to the user control dynamically in XAML.

相关标签:
1条回答
  • 2021-01-18 21:54

    You can do that, although not quite like your example. You need two things. The first is to declare a DependencyProperty of type UIElement, of which all controls extend:

    public static DependencyProperty InnerContentProperty = DependencyProperty.Register("InnerContent", typeof(UIElement), typeof(YourControl));
    
    public UIElement InnerContent
    {
        get { return (UIElement)GetValue(InnerContentProperty); }
        set { SetValue(InnerContentProperty, value); }
    }
    

    The second is to declare a ContentControl in the XAML where you want the content to appear:

    <StackPanel>
        <TextBlock Text="I want the user to be able to add any number of controls to the  StackPanel below this TextBlock."
                   FontFamily="Arial" FontSize="12" FontWeight="DemiBold" Margin="5,10,5,10" TextWrapping="Wrap"/>
        <StackPanel>
            <ContentControl Content="{Binding InnerContent, RelativeSource={RelativeSource AncestorType={x:Type YourXmlNamspacePrefix:ContentView}}}" />
        </StackPanel>
    </StackPanel>
    

    In my opinion, if you use StackPanels, you could find that your content does not get displayed correctly... I'd advise you to use Grids for layout purposes for all but the simplest layout tasks.

    Now the one difference to your example is in how you would use your control. The InnerContent property is of type UIElement, which means that it can hold one UIElement. This means that you need to use a container element to display more than one item, but it has the same end result:

    <YourXmlNamspacePrefix:YourControl>
        <YourXmlNamspacePrefix:YourControl.InnerContent>
            <StackPanel x:Name="MyUserControl_Test" Margin="10" Height="100">
                <Button Content="Click" Height="30" Width="50"/>
                <Button Content="Click" Height="30" Width="50"/>
                <Button Content="Click" Height="30" Width="50"/>
            </StackPanel>
        </YourXmlNamspacePrefix:YourControl.InnerContent>
    </YourXmlNamspacePrefix:YourControl>
    

    And the result:

    enter image description here


    UPDATE >>>

    For the record, I know exactly what you want to do. You, it seems, do not understand what I am saying, so I'll try to explain it one last time for you. Add a Button with the Tag property set as I've already shown you:

    <Button Tag="MyButton1" Content="Click" Click="ButtonClick" />
    

    Now add a Click handler:

    private void ButtonClick(object sender, RoutedEventArgs e)
    {
        Button button = (Button)sender;
        if (button.Tag = "MyButton1") DoSomething();
    }
    

    That's all there is to it.

    0 讨论(0)
提交回复
热议问题