Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DependencyProperty编译并工作,但Intellisense不';我不这么认为_C#_Wpf_Visual Studio 2012_Wpf Controls_Wpf 4.0 - Fatal编程技术网

C# DependencyProperty编译并工作,但Intellisense不';我不这么认为

C# DependencyProperty编译并工作,但Intellisense不';我不这么认为,c#,wpf,visual-studio-2012,wpf-controls,wpf-4.0,C#,Wpf,Visual Studio 2012,Wpf Controls,Wpf 4.0,我有两个自定义控件/视觉效果,两个控件上都需要Orientation属性。在这两种情况下,默认值应为“水平”,但控件/视觉的用户应能够指定Orientation=“Vertical”,以垂直排列控件/视觉的组件。我所拥有的东西在ImageButton控件上效果很好,但在我的HeaderedLabel visual上效果不太好。虽然这两个都编译得很好,但Intellisense不喜欢其中一个。这里有一个使用它们的例子 <Visuals:ImageButton Image="Icons/ok.

我有两个自定义控件/视觉效果,两个控件上都需要
Orientation
属性。在这两种情况下,默认值应为
“水平”
,但控件/视觉的用户应能够指定
Orientation=“Vertical”
,以垂直排列控件/视觉的组件。我所拥有的东西在ImageButton控件上效果很好,但在我的HeaderedLabel visual上效果不太好。虽然这两个都编译得很好,但Intellisense不喜欢其中一个。这里有一个使用它们的例子

<Visuals:ImageButton Image="Icons/ok.png" Content="Normal Content"/>
<Visuals:ImageButton Image="Icons/ok.png" Content="Vertical Content" Orientation="Vertical"/>
<Visuals:HeaderedLabel Header="Normal Header" Content="Normal Content"/>
<Visuals:HeaderedLabel Header="Vertical Header" Content="Vertical Content" Orientation="Vertical"/>


你知道为什么编译器会将ImageButton的
Orientation.Horizontal
解析为System.Windows.Controls.Orientation.Horizontal,而不是HeaderedLabel吗?更重要的是,有什么想法可以解释为什么Intellisense不能为HeaderedLabel.Orientation找到选择吗


顺便说一句,我使用的是VisualStudio 2012和.NET Framework 4.0。

您的所有属性,包括
方向
属性,都声明为具有类型
对象

您应该改为:

public Orientation Orientation
{
    get { return (Orientation)GetValue(OrientationProperty); }
    set { SetValue(OrientationProperty, value); }
}
如果正确声明属性,XAML编辑器应该能够正确接受
Orientation
类型的值。否则,它将尝试将
字符串
“Vertical”
分配给属性,当传递给
SetValue()
方法时,该属性将失败,因为
dependencProperty
对象本身是以
Orientation
作为有效类型初始化的,并且它无法从
字符串
值转换为
Orientation


如果正确声明属性,则WPF将自动理解它需要将XAML中显示的
字符串
值转换为属性的
方向
值(即将
字符串
值解析为适当的
枚举
类型),在这种情况下,初始化应该可以工作。

无法重现您在有关依赖项属性声明的评论中提到的typeof(System.Windows.Controls.Orientation)问题,但对于xaml Intellisense,解决方案很简单:仔细查看您的普通属性声明。在ImageButton中您有
方向方向
(右),在HeaderedLabel中您有
对象方向
(错误)谢谢,这就是问题所在。愚蠢的剪切粘贴错误。关于其他属性,
标题
内容
,它们应该是其他属性,还是类型
对象
是最佳选择?(它们都只是用户想要的任意形式的字符串。)另外,既然你没有伤害到rep,你是否希望我删除这个问题,因为它只是一个剪切粘贴错误?这个问题可以在“排版错误”的原因下结束,所以删除可能是合适的。无论我有多少代表,这都是事实。:)所以,如果您觉得问题的解决方式不太可能对以后搜索堆栈溢出的其他人有帮助,请随意这样做。我会注意到,你的问题是唯一的一个短语,“不是有效的财产价值”和“智能感知”一词,所以要考虑其他人是否会犯类似的错误,他们是否可能通过搜索找到这一点。
// File 'HeaderedLabel.cs'
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Visuals
{
   public class HeaderedLabel : Control
   {
      static HeaderedLabel()
      {
         DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderedLabel),
            new FrameworkPropertyMetadata(typeof(HeaderedLabel)));
      }

      public object Header
      {
         get { return (object)GetValue(HeaderProperty); }
         set { SetValue(HeaderProperty, value); }
      }

      public object Content
      {
         get { return (object)GetValue(ContentProperty); }
         set { SetValue(ContentProperty, value); }
      }

      public object Orientation
      {
         get { return (object)GetValue(OrientationProperty); }
         set { SetValue(OrientationProperty, value); }
      }

      public static readonly DependencyProperty HeaderProperty =
         DependencyProperty.Register("Header", typeof(object), typeof(HeaderedLabel),
            new UIPropertyMetadata(null));

      public static readonly DependencyProperty ContentProperty =
         DependencyProperty.Register("Content", typeof(object), typeof(HeaderedLabel),
            new UIPropertyMetadata(null));

      // Note that for HeaderedLabel, unlike ImageButton, I have to specify the fully-
      // qualified name of 'Orientation.Horizontal', otherwise the compiler resolves it
      // to Visuals.HeaderedLabel.Orientation and gives a compiler error...
      public static readonly DependencyProperty OrientationProperty =
         DependencyProperty.Register("Orientation", typeof(System.Windows.Controls.Orientation),
            typeof(HeaderedLabel), new UIPropertyMetadata(System.Windows.Controls.Orientation.Horizontal));
   }
}
   <!-- File '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:Visuals"
                        xmlns:bind="clr-namespace:Visuals.BindingConverters">

       <Style TargetType="{x:Type local:ImageButton}">
          <Setter Property="Template">
             <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ImageButton}">
                   <Button>
                      <StackPanel Orientation="{TemplateBinding Orientation}">
                         <Image Source="{TemplateBinding Image}"/>
                         <ContentPresenter/>
                      </StackPanel>
                   </Button>
                </ControlTemplate>
             </Setter.Value>
          </Setter>
       </Style>

       <Style TargetType="{x:Type local:HeaderedLabel}">
          <Setter Property="Template">
             <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:HeaderedLabel}">
                   <StackPanel Orientation="{TemplateBinding Orientation}">
                      <StackPanel Orientation="Horizontal">
                         <ContentControl Content="{TemplateBinding Header}" />
                         <TextBlock Text=":" />
                      </StackPanel>
                      <ContentControl Content="{TemplateBinding Content}"/>
                   </StackPanel>
                </ControlTemplate>
             </Setter.Value>
          </Setter>
       </Style>

    </ResourceDictionary>
public Orientation Orientation
{
    get { return (Orientation)GetValue(OrientationProperty); }
    set { SetValue(OrientationProperty, value); }
}