通过依赖属性替换用户控件中的控件

发布于 11-17 13:06 字数 1966 浏览 3 评论 0原文

我做了一个简单的项目来说明我的问题。我有一个用户控件(“ButtonPod”),其中包含一个按钮和一个矩形:

<UserControl x:Class="SilverlightDependencyProp.ButtonPod"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <Rectangle Fill="Blue" />
    <Button x:Name="ButtonOnPage" Margin="50" Content="Old button content" />
</Grid>

</UserControl>

我想在整个应用程序中使用此用户控件,但我需要更改中间的按钮。我需要控制 Button 的所有属性,因此我不想只公开“ButtonText”或“ButtonSize”等 DependencyProperties - 我宁愿在使用该控件时定义整个 Button。因此,我设置了一个依赖属性(“CenterButton”),如下所示:

    public Button CenterButton
    {
        get { return (Button)GetValue(CenterButtonProperty); }
        set { SetValue(CenterButtonProperty, value); }
    }

    public static readonly DependencyProperty CenterButtonProperty =
        DependencyProperty.Register("CenterButton", typeof(Button),
        typeof(ButtonPod), new PropertyMetadata(
        CenterButtonChanged));

    private static void CenterButtonChanged(DependencyObject d,
      DependencyPropertyChangedEventArgs e)
    {
        var pod = d as ButtonPod;
        pod.ButtonOnPage = e.NewValue as Button;
    }

然后,我尝试在用户控件内的 MainPage.xaml 上定义“CenterButton”:

<Grid x:Name="LayoutRoot" Background="White">
    <local:ButtonPod Width="200" Height="200">
        <local:ButtonPod.CenterButton>
            <Button Content="New button content" />   
        </local:ButtonPod.CenterButton>
    </local:ButtonPod>
</Grid>

但是当我加载应用程序时,我看到的只是一个按钮,上面写着“旧按钮内容”——为什么我的按钮没有被替换?逐步执行,我可以看到 DependencyProperty 被命中并且“ButtonOnPage”属性被设置,但可视化树没有更新。有什么想法吗?

I've made a simple project to illustrate my problem. I have a user control ('ButtonPod') that houses a button and a rectangle:

<UserControl x:Class="SilverlightDependencyProp.ButtonPod"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <Rectangle Fill="Blue" />
    <Button x:Name="ButtonOnPage" Margin="50" Content="Old button content" />
</Grid>

</UserControl>

I want to use this user control throughout my application, but I need to change the Button in the middle. I need control over all the properties of the Button, so I don't want to just expose DependencyProperties like 'ButtonText' or 'ButtonSize' - I would rather define the entire Button whenever I use the control. So I set up a dependency property ('CenterButton') like this:

    public Button CenterButton
    {
        get { return (Button)GetValue(CenterButtonProperty); }
        set { SetValue(CenterButtonProperty, value); }
    }

    public static readonly DependencyProperty CenterButtonProperty =
        DependencyProperty.Register("CenterButton", typeof(Button),
        typeof(ButtonPod), new PropertyMetadata(
        CenterButtonChanged));

    private static void CenterButtonChanged(DependencyObject d,
      DependencyPropertyChangedEventArgs e)
    {
        var pod = d as ButtonPod;
        pod.ButtonOnPage = e.NewValue as Button;
    }

Then I try defining the 'CenterButton' on my MainPage.xaml, within my user control:

<Grid x:Name="LayoutRoot" Background="White">
    <local:ButtonPod Width="200" Height="200">
        <local:ButtonPod.CenterButton>
            <Button Content="New button content" />   
        </local:ButtonPod.CenterButton>
    </local:ButtonPod>
</Grid>

But when I load up the app, all I see is a button that says "Old button content" -- why isn't my button being replaced? Stepping through, I can see that the DependencyProperty gets hit and the 'ButtonOnPage' property gets set, but the visual tree does not update. Any ideas?

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

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

发布评论

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

评论(1

绝情姑娘2024-11-24 13:06:49

设置命名元素不会替换它。您想要命名父级并将子级添加到其中(替换现有的子级)。

Setting a named element does not replace it. You want to name the parent and add the child to that instead (replacing the existing children).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文