新しいWPFウィンドウクラスのスタイルを作成し、そこにいくつかの依存プロパティを作成しました。注意すべき1がShowHelpButton依存関係プロパティがVisual Studio Designerを更新しない
ある
これは、ウィンドウ上の[ヘルプ]ボタンの表示を切り替えることになっています。コードは、実行時に正常に動作しますが、デザインビューでUIを更新することができません。
ここでは、クラスの:ここで
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>
そして最後に、ここで私はそれを使用している方法は次のとおりです。
<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を追加しました:
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsParentMeasure |
FrameworkPropertyMetadataOptions.AffectsParentArrange
は私も無駄にコールバックを追加しました。私はVisual Studio 2015を再起動しようとしました。私はVSバグだと思っていますが、何が起こっているのか誰かが考えていることを願っています。助けてくれてありがとう!
テンプレートバインディングの代わりにcontroltemplateでトリガを使用して、それが役立つかどうかを確認してください。 – Liero