0
给定一个非常简单的自定义容器控件CustomDock,它具有两个附加属性IsFooBar1和IsFooBar2。如何设置IsFooBar2更新IsFooBar1的值时,如何确保Visual Studio更新IsFooBar1的生成xaml值。WPF附加的依赖项属性计算的字段更新
自定义控件:
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);
}
}
及其在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>
在Visual Studio的设计,如果IsFooBar2更改为false,那么IsFooBar1也应给予一个错误的值,但它不是,无论是在属性面板还是在xaml代码中。
Visual Studio不会在设计时连续执行您的代码。它第一次加载,就是这样。如果你有Blend,那么你可能会看到它......但是你为什么想要像这样改变设计时间的价值? – mlemay 2012-07-06 11:43:08
我正在通过属性窗格更改IsFooBar2,就像我为任何其他属性一样。 IsFooBar2在内部更改IsFooBar1的值。此更改不会反映在xaml或属性窗格中。 Visual Studio不需要连续执行代码。 – DAC 2012-07-06 11:48:22
而在运行时,xaml会更新为良好的值?它只是在设计阶段吗? – mlemay 2012-07-06 11:51:17