C# 依赖项属性未更新Visual Studio Designer

C# 依赖项属性未更新Visual Studio Designer,c#,wpf,visual-studio-2015,dependency-properties,C#,Wpf,Visual Studio 2015,Dependency Properties,我已经为一个新的WPF窗口类创建了一个样式,并且在其中有一些依赖属性。需要注意的是 显示帮助按钮 这将切换窗口上“帮助”按钮的可见性。代码在运行时运行良好,但我无法让它在设计视图中更新UI 下面是课程: public class MainWindowFrame : Window { #region DependencyProperties public static readonly DependencyProperty ShowHelpButtonProperty = Depende

我已经为一个新的WPF窗口类创建了一个样式,并且在其中有一些依赖属性。需要注意的是

显示帮助按钮

这将切换窗口上“帮助”按钮的可见性。代码在运行时运行良好,但我无法让它在设计视图中更新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>

我还添加了一个回调,但没有用。我甚至尝试重新启动Visual Studio 2015。我开始认为这只是一个VS错误,但我希望有人知道发生了什么。谢谢你的帮助

对不起,我无法重现您的问题

我通常用一种更简单但仍然复杂的方式。我将ViewModel放在另一个程序集中,因此我不会从view中引用它为了使所有内容都可设置样式,我将模板放入Generic.xaml中,并覆盖另一个字典中的样式,该字典在App.xaml中的Generic之后加载 这是我做事的方式

MyWindow.xaml:

<Window x:Class="Sandbox.MyWindow"
    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"
    xmlns:local="clr-namespace:Sandbox"
    xmlns:l="clr-namespace:ProjectLibrary;assembly=ProjectLibrary"
    mc:Ignorable="d" d:DataContext="{DynamicResource DesignViewModel}"
    Title="MyWindow" Height="300" Width="300">
<Window.Resources>
    <l:MyViewModel x:Key="DesignViewModel" SomeButtonVisibility="Collapsed"/>
</Window.Resources>
<StackPanel>
    <TextBlock Text="{Binding SomeText}"/>
    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="1 Button"/>
    <Button Visibility="{Binding SomeButtonVisibility}" HorizontalAlignment="Center" VerticalAlignment="Center" Content="2 Button"/>
    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="3 Button"/>
</StackPanel>
MyViewModel:

using System.Windows;

namespace ProjectLibrary
{
public class MyViewModel : Notifiable
{
    public MyViewModel() :this(false) {
    }

    public MyViewModel(bool Execute) {
        if (Execute) {
            SomeText = "Execution data";
        } else {
            SomeText = "Design Data";
        }
        SomeButtonVisibility = Visibility.Visible;
    }

    private string _someText;
    public string SomeText { get { return _someText; } set { _someText = value; RaisePropertyChanged("SomeText"); } }

    private Visibility _someButtonVisibility;
    public Visibility SomeButtonVisibility { get { return _someButtonVisibility; } set { _someButtonVisibility = value; RaisePropertyChanged("SomeButtonVisibility"); } }
 }
}
Notifiable.cs:

using System.ComponentModel;

namespace PTR.PTRLib.Common {
public class Notifiable : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName) {
        // take a copy to prevent thread issues
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 }
}
App.xaml:

<Application x:Class="Sandbox.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:Sandbox">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Themes/Generic.xaml"/> <!-- Default Styles -->
            <ResourceDictionary Source="Themes/StyleRes.xaml"/> <!-- ColorTemplates -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

模板/Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Sandbox">
<Style TargetType="{x:Type local:MyWindow}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
            <Setter Property="Template" Value="{StaticResource WindowTemplateKey}"/>
        </Trigger>
    </Style.Triggers>
</Style>

Themes/StyleRes.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Sandbox">

<Style TargetType="{x:Type local:MyWindow}">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="Background" Value="LightBlue"/>
</Style>
<Style TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="Background" Value="LightBlue"/>
</Style>


我不是WPF方面的专业人士,但这是我在1.5年的数据库+WPF接口编程课程(不错的一个)中学到的。

我知道这可能是一个简短的答案。。。。但是我认为这个问题,因为它只是在设计时出现的,可能是由于设计器中的数据上下文问题。你试过d:DataContext吗

 d:DataContext ="{d:DesignInstance {x:Type nameSpace:ViewModel}, IsDesignTimeCreatable=True}"

更新答案

它看起来像是一个已知的设计时错误;对于子类/派生窗口对象 此报告的错误似乎与此问题有关:

我们无法在设计器中创建Window的设计实例,因此我们使用自己的代理类型进行替换

因此,如果设计器无法创建派生窗口类型的实例;绑定(TemplateBinding)逻辑将在设计时失败


由于向TemplateBinding提供回退值并不容易,所以您可以使用它来提供默认值,作为设计时行为

这与DataContext无关。TemplateBinding就是为了这个目的。只是再次检查了你的代码;我错过了模板绑定。我会更新我的答案。这就是我的想法(并且害怕哈哈)。感谢您确认这是一个VS错误。我希望这不是一条评论所暗示的设计意图。也许有一天它会被修复。试着用controltemplate中的触发器代替templatebinding,看看它是否有助于。。。正如Sharada Gururaj所说,这是一个已知的bug。现在我记得我以前处理过类似的事情,但没有找到解决办法。所以我开始在用户控件中做所有的事情,但现在我意识到你可以用我的模式轻松地做自定义控件。您可以将每个控件原型化为UserControl,然后复制/粘贴并创建CustomControl。以后制作模板和重用控件将很容易。谢谢您的回复!这可能是一种解决方法,但唯一的问题是,设计MyWindow的所有代码都必须在.cs文件中完成,因为我希望在另一个包含xaml代码的类中继承MyWindow(不允许继承在另一个xaml文件中使用xaml定义的类)。我真的不想在cs文件中创建所有UI代码。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Sandbox">
<Style TargetType="{x:Type local:MyWindow}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
            <Setter Property="Template" Value="{StaticResource WindowTemplateKey}"/>
        </Trigger>
    </Style.Triggers>
</Style>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Sandbox">

<Style TargetType="{x:Type local:MyWindow}">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="Background" Value="LightBlue"/>
</Style>
<Style TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="Background" Value="LightBlue"/>
</Style>
 d:DataContext ="{d:DesignInstance {x:Type nameSpace:ViewModel}, IsDesignTimeCreatable=True}"