依赖项属性未更新Visual Studio Designer [英] Dependency Property Not Updating Visual Studio Designer
问题描述
我为新的WPF Window类创建了一种样式,并在其中具有一些依赖项属性.要注意的是
I've created a style for a new WPF Window class and have some dependency properties on there. The one to note is
ShowHelpButton
ShowHelpButton
这应该在窗口上切换帮助"按钮的可见性.该代码在运行时可以正常工作,但是我无法在设计视图中更新它.
This is supposed to toggle the visibility of the Help button on the window. The code works fine in runtime, but I cannot get it to update the UI in the design view.
这是课程:
public class MainWindowFrame : Window
{
#region DependencyProperties
public static readonly DependencyProperty ShowHelpButtonProperty = DependencyProperty.Register(
"ShowHelpButton", typeof (bool), typeof (MainWindowFrame), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
public bool ShowHelpButton
{
get { return (bool) GetValue(ShowHelpButtonProperty); }
set { SetValue(ShowHelpButtonProperty, value); }
}
#endregion
static MainWindowFrame()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MainWindowFrame),
new FrameworkPropertyMetadata(typeof(MainWindowFrame)));
}
这是样式:
<Style x:Key="MainWindowStyle" TargetType="{x:Type abstractClasses:MainWindowFrame}">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="AllowsTransparency" Value="True" />
<Setter Property="Background" Value="{StaticResource LightBlueBrush}" />
<Setter Property="BorderBrush" Value="{StaticResource BlueBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="1" />
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Title" Value="New Window" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type abstractClasses:MainWindowFrame}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}">
<Grid x:Name="ContainerGrid" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.Triggers>
<EventTrigger RoutedEvent="Grid.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="0"
To="1"
Duration="00:00:01" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid Background="Transparent" MouseDown="Window_MouseDownDrag">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<TextBlock
Margin="10,3,0,3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Style="{StaticResource CustomTitleBarTextBlackB}"
Text="{TemplateBinding Title}" />
</Grid>
<Button
Grid.Column="1"
Width="20"
Height="20"
Margin="0,0,5,0"
HorizontalAlignment="Right"
AutomationProperties.AutomationId="Help"
Style="{StaticResource HelpButtonStyle}"
Visibility="{TemplateBinding Property=ShowHelpButton,
Converter={StaticResource BoolToVisConverter}}" />
</Grid>
<AdornerDecorator Grid.Row="1">
<ContentPresenter x:Name="WindowContent" />
</AdornerDecorator>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
最后,这是我的使用方式:
And finally, here's how I'm using it:
<abstractClasses:MainWindowFrame
x:Class="Utils.UI.NewFeaturesDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:abstractClasses="clr-namespace:Utils.AbstractClasses"
xmlns:ui="clr-namespace:Utils.UI"
xmlns:utilResx="clr-namespace:Utils.Resources"
Width="775"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
ShowHelpButton="False"
SizeToContent="Height"
Style="{DynamicResource ResourceKey=MainWindowStyle}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Utils;component/WPFStyles/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
</abstractClasses:MainWindowFrame>
我似乎已经尝试了一切.我通过执行以下操作添加了所有FrameworkPropertyMetadataOptions:
I've seemingly tried everything. I've added all the FrameworkPropertyMetadataOptions by doing this:
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsParentMeasure |
FrameworkPropertyMetadataOptions.AffectsParentArrange
我也添加了一个回调,但无济于事.我什至尝试重新启动Visual Studio2015.我开始认为这只是一个VS错误,但我希望有人对发生的事情有所了解.感谢您的帮助!
I've also added a callback to no avail. I've even tried restarting Visual Studio 2015. I'm starting to think it's just a VS bug, but I'm hoping someone has some idea of what's going on. Thanks for any help!
推荐答案
更新后的答案
它看起来像是一个已知的设计时错误;用于子类/派生的Window对象所报告的错误似乎与以下问题有关: WPF设计器不显示分配给自定义DependencyProperty的内容
It looks like its a known design-time bug; for subclassed/derived Window objects This reported bug seems to be related to this issue: WPF designer not showing content assigned to custom DependencyProperty
我们无法在设计器中创建Window的设计实例,因此我们用自己的代理类型代替.
We cannot create a design instance of Window within the designer so we substitute with a proxy type of our own.
因此,如果设计人员无法创建派生窗口类型的实例;绑定(TemplateBinding)逻辑将在设计时失败.
So if the designer can't create an instance of derived window type; the binding (TemplateBinding) logic will fail during design-time.
由于不容易为TemplateBinding提供后备值,因此您可以使用此方法为以下内容提供默认值:充当设计时行为.
As it is not easy to provide fallback values to TemplateBinding, you can maybe use this approach to provide default values to act as design-time behavior.
这篇关于依赖项属性未更新Visual Studio Designer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!