0

Given a very simple custom container control CustomDock with two attached properties, IsFooBar1 and IsFooBar2. How can I ensure that visual studio will update the generated xaml for the values of IsFooBar1 if setting IsFooBar2 updates the value of IsFooBar1 when it is being changed.

The custom control:

    public class CustomDock : DockPanel
    {
    public static readonly DependencyProperty IsFooBarProperty1 = DependencyProperty.RegisterAttached(
      "IsFooBar1",
      typeof(Boolean),
      typeof(CustomDock),
      new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
    );

    public static void SetIsFooBar1(UIElement element, Boolean value)
    {
        element.SetValue(IsFooBarProperty1, value);
    }

    public static Boolean GetIsFooBar1(UIElement element)
    {
        return (Boolean)element.GetValue(IsFooBarProperty1);
    }

    public static readonly DependencyProperty IsFooBarProperty2 = DependencyProperty.RegisterAttached(
      "IsFooBar2",
      typeof(Boolean),
      typeof(CustomDock),
      new PropertyMetadata(false)
    );

    public static void SetIsFooBar2(UIElement element, Boolean value)
    {
        element.SetValue(IsFooBarProperty2, value);
        element.SetValue(IsFooBarProperty1, value);

    }

    public static Boolean GetIsFooBar2(UIElement element)
    {
        return (Boolean)element.GetValue(IsFooBarProperty2);
    }

}

And its use in xaml:

<Window x:Class="TestAttachedIndirectProperties.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:TestAttachedIndirectProperties">
<Grid>
    <my:CustomDock Height="100" HorizontalAlignment="Left" Margin="138,123,0,0" x:Name="customDock1" VerticalAlignment="Top" Width="200">
        <Button Content="Button" Height="23" Name="button1" Width="75" my:CustomDock.IsFooBar1="True" my:CustomDock.IsFooBar2="True" />
    </my:CustomDock>
</Grid>

During Visual Studio design, if IsFooBar2 is changed to false, then IsFooBar1 should also be given a false value, but it is not, niether in the properties pane or in the xaml code.

DAC
  • 1,769
  • 1
  • 20
  • 32
  • Visual Studio does not execute continuously your code in design-time. It load the first time and that's it. If you have Blend, then yes you will probably see it... But why do you want to change the value in design time like this? – mlemay Jul 06 '12 at 11:43
  • I am changing IsFooBar2 via the properties pane, just as I would for any other property. IsFooBar2 is internally changing the value of IsFooBar1. This change is not reflected in the xaml, or in the properties pane. Visual Studio does not need to execute the code continuously. – DAC Jul 06 '12 at 11:48
  • And at run-time, the xaml is updated to the good value? Is it just at design-time? – mlemay Jul 06 '12 at 11:51

1 Answers1

1

WPF makes no guarantees that your Dependency property setter will be called (as strange as that is) if you want to set up dependent properties you need to do it my registering an OnPropertyChanged callback via the property definition and put the cascade logic there.

Adam Mills
  • 7,719
  • 3
  • 31
  • 47