依赖项属性不更新Visual Studio Designer

时间:2017-04-15 17:47:49

标签: c# wpf visual-studio-2015 dependency-properties

我为新的WPF Window类创建了一个样式,并在那里有一些依赖属性。需要注意的是

  

ShowHelpButton

这应该切换窗口上“帮助”按钮的可见性。代码在运行时工作正常,但我无法在设计视图中更新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错误,但我希望有人知道发生了什么。谢谢你的帮助!

3 个答案:

答案 0 :(得分:2)

更新回答

它看起来像是一个已知的设计时错误;用于子类化/派生的Window对象 此报告的错误似乎与此问题有关:WPF designer not showing content assigned to custom DependencyProperty

  

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

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

由于向TemplateBinding提供回退值并不容易,您可以使用此approach来提供默认值以充当设计时行为。

答案 1 :(得分:0)

抱歉,我无法重新创建您的问题。

我通常采用更简单但更复杂的方式。我把ViewModel放在另一个程序集中,所以我没有诱惑从视图中引用它。 +为了使一切都可以设置样式,我将模板放入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>

在App.xaml中,我删除了StartupURI以在App.xaml.cs中处理它:

using ProjectLibrary;
using System.Windows;

namespace Sandbox {
public partial class App : Application {
    MyWindow w;
    MyViewModel vm;

    public App() {
        w = new MyWindow();
        //You also can pass Action to open new window of some sort here
        //or other things, that VM can't have access to
        vm = new MyViewModel(true);
        w.DataContext = vm;

        w.Show();
    }
  }
}

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>

主题/ 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界面编程课程中学到的(很好的)。

答案 2 :(得分:0)

我知道这可能是一个简短的答案....但我正在考虑这个问题,因为它只是在设计时,可能是由于设计师的数据上下文问题。你试过d:DataContext吗?

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

https://www.codeproject.com/tips/879109/using-design-time-databinding-while-developing-a-w

相关问题