1

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?

Nick B
  • 1,101
  • 9
  • 19

1 Answers1

1

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

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • Right, but doesn't replacing with the new button (via pod.ButtonOnPage = e.NewValue as Button;) affect the visual tree? The property is just the way to set the new button.. or so I thought. – Nick B Jun 29 '11 at 18:24
  • Thanks -- the problem is that x:Name just makes a reference to what's on the page, which you can't change.. basically like a get without a set. Thanks for the help. – Nick B Jun 29 '11 at 18:32
  • 1
    @Nick Bradley: If you have a look at the generated designer code, you will see that it has a member per named element. These are connected to the Xaml, but are still also just properties. By setting a named property you are just replacing a class member, but are not connecting it to the visual tree. I guess those properties should all be marked 'readonly' – iCollect.it Ltd Jun 30 '11 at 07:25