如何实现内部内容依赖属性?

发布于 2025-01-08 21:57:49 字数 397 浏览 3 评论 0原文

我正在尝试实现具有依赖属性的用户控件。这是我的问题;我想用用户控件的一个或多个布局子项设置依赖属性。这可能吗?怎样才能做到?

    <custom:myControl1>
        <Label>Controls</Label>
        <Label>I want</Label>
        <Label>to set</Label>
        <Label>as the dependency property</Label>
        <Button Content="Is it possible?" />
    </custom:myControl1>

I'm trying to implement a usercontrol with dependency properties. Here is my question; i want to set a dependency property with layout child or children of my user control. Is it possible and how can it be done?

    <custom:myControl1>
        <Label>Controls</Label>
        <Label>I want</Label>
        <Label>to set</Label>
        <Label>as the dependency property</Label>
        <Button Content="Is it possible?" />
    </custom:myControl1>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单气质女生网名 2025-01-15 21:57:49

是的,在 UserControl 的 XAML 中声明 ContentControl
让它将其 Content 属性绑定到 UserControl 代码隐藏的 DependencyProperty
在 UserControl 类顶部添加属性:[ContentProperty("Name_Of_Your_Dependency_Property")]

然后你就可以完全按照你在问题中所做的那样做。该属性定义默认的依赖属性,以便您不必指定

像这样的东西:

[ContentProperty("InnerContent")]
public class MyControl : UserControl
{
   #region InnerContent
        public FrameworkElement InnerContent
        {
            get { return (FrameworkElement)GetValue(InnerContentProperty); }
            set { SetValue(InnerContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for InnerContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InnerContentProperty =
            DependencyProperty.Register("InnerContent", typeof(FrameworkElement), typeof(MyControl), new UIPropertyMetadata(null));
        #endregion
}

<UserControl ...>
   <ContentControl Content="{Binding InnerContent, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
</UserControl>

Yes, declare a ContentControl in the XAML of your UserControl.
Make it bind its Content property to a DependencyProperty on the code-behind of your UserControl.
Add the attribute: [ContentProperty("Name_Of_Your_Dependency_Property")] on top of your UserControl class.

Then you can do precisely as you did in your question. The attribute defines the default Dependency Property so that you dont have to specify <custom:myControl1.MyDP>.

Something like:

[ContentProperty("InnerContent")]
public class MyControl : UserControl
{
   #region InnerContent
        public FrameworkElement InnerContent
        {
            get { return (FrameworkElement)GetValue(InnerContentProperty); }
            set { SetValue(InnerContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for InnerContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InnerContentProperty =
            DependencyProperty.Register("InnerContent", typeof(FrameworkElement), typeof(MyControl), new UIPropertyMetadata(null));
        #endregion
}

<UserControl ...>
   <ContentControl Content="{Binding InnerContent, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
</UserControl>
久随 2025-01-15 21:57:49

按预期工作。

  1. 创建了一个 UserControl 并添加了一个绑定 DP 的 ContentControl。 (按照上面代码后面的解决方案中所述创建DP。
<UserControl x:Class="WpfTry2.Controls.DummyContentControl"
... x:Name="dummyContent">
   <Grid>
        <ContentControl Content="{Binding InnerContent, ElementName=dummyContent}"/>
   </Grid>
</UserControl>
  1. 使用Window中的UserControl
<controls:DummyContentControl>
            <controls:DummyContentControl.InnerContent>
                <Grid Background="Aqua" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <TextBlock Text="InnerContent" FontSize="32"></TextBlock>
                </Grid>
            </controls:DummyContentControl.InnerContent>
</controls:DummyContentControl>

Working as expected.

  1. Created a UserControl and added a ContentControl binding the DP. (Create DP as said in above solution in code behind.
<UserControl x:Class="WpfTry2.Controls.DummyContentControl"
... x:Name="dummyContent">
   <Grid>
        <ContentControl Content="{Binding InnerContent, ElementName=dummyContent}"/>
   </Grid>
</UserControl>
  1. Used the UserControl in Window
<controls:DummyContentControl>
            <controls:DummyContentControl.InnerContent>
                <Grid Background="Aqua" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <TextBlock Text="InnerContent" FontSize="32"></TextBlock>
                </Grid>
            </controls:DummyContentControl.InnerContent>
</controls:DummyContentControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文