简体   繁体   English

WPF:无法识别或无法从XAML访问整数CLR属性

[英]WPF: Integer CLR property not recognized or is not accessible from XAML

I have a custom control in WPF. 我在WPF中有一个自定义控件。 I added two plain CLR property, that look like this: 我添加了两个普通的CLR属性,如下所示:

private int _ChordFrom = 0;
public int ChordFrom
{
    get { return _ChordFrom; }
    set { _ChordFrom = value; }
}

private int _ChordTo = 0;
public int ChordTo
{
    get { return _ChordTo; }
    set { _ChordTo = value; }
}

I use plain CLR properties, because I don't want to bind these properties to any other dependency. 我使用普通的CLR属性,因为我不想将这些属性绑定到任何其他依赖项。 I just want to set the values inside XAML, when the control is instantiated. 我只是想在实例化控件时在XAML中设置值。

Inside XAML the properties are found by intellisense, but when building the project I get the error: 在XAML中,属性是由intellisense找到的,但在构建项目时我得到错误:

"The member "ChordFrom" is not recognized or is not accessible." “会员”ChordFrom“无法识别或无法访问。” "The member "ChordTo" is not recognized or is not accessible." “会员”ChordTo“无法识别或无法访问。”

The XAML code looks like this: XAML代码如下所示:

<TabItem Header="CHORDS I">
    <Grid x:Name="_gridChords_1" Background="AliceBlue">
        <mg:MidiChordGrid x:Name="gridMidiChordGrid_1" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  ChordFrom="0" ChordTo="7"/>
    </Grid>
</TabItem>

There are three things, I wonder about: 有三件事,我想知道:

  • Why does Intellisense offers me the the properties inside XAML, if they are not accessible? 为什么Intellisense会向我提供XAML中的属性,如果它们不可访问?

  • If I close all XAML windows, rebuild the project, the errors disappear. 如果我关闭所有XAML窗口,重建项目,错误就会消失。

  • Even if can build the project without any errors, the properties are not set. 即使可以在没有任何错误的情况下构建项目,也不会设置属性。

ANSWER | 答案| ANSWER | 答案| ANSWER | 答案| ANSWER | 答案| ANSWER | 答案| ANSWER | 答案| ANSWER 回答

Thanks for all the answers, but the solution/error was inside my code. 感谢所有答案,但解决方案/错误在我的代码中。 I forgot to update my grid after setting these two CLR properties. 设置这两个CLR属性后,我忘了更新我的网格。 Sorry for this. 非常遗憾。

But here some facts you may want to know: 但这里有一些你可能想知道的事实:

  • You can use and set plain CLR properties inside WPF XAML. 您可以在WPF XAML中使用和设置纯CLR属性。 I checked it out and the properties are set from XAML 我检查了它,属性是从XAML设置的
  • The error "not recognized or is not accessible" seems to be a VS bug, because the project is buildable 错误“无法识别或无法访问”似乎是一个VS错误,因为该项目是可构建的
  • If you close the XAML files inside VS there will be no build errors anymore 如果关闭VS中的XAML文件,将不再存在构建错误

To me, using plain CLR properties is the best solution at this point, because the values I pass to the control are hardcoded for every instance of the control and they do not change at runtime. 对我来说,使用普通CLR属性是此时的最佳解决方案,因为我传递给控件的值是针对控件的每个实例进行硬编码的,并且它们在运行时不会更改。 Of course I would want to use attached DPs, if the values changed at runtime, which they don't. 当然,我想使用附加的DP,如果值在运行时更改,他们不会。

Thank for your quick answers, but it in the end it was a my fault. 感谢您的快速解答,但最终这是我的错。

I am just confirming that this appears to be a visual studio bug as mentioned in the question. 我只是确认这似乎是问题中提到的视觉工作室错误。 I added a couple properties to a control and spent an hour trying to figure out why it would not build. 我向控件添加了几个属性,花了一个小时试图找出它不会构建的原因。 After reading the above post I simply closed the xaml file, clicked build and it built successfully. 阅读上面的帖子后,我只需关闭xaml文件,点击构建并成功构建。 Using VS 2013 community sp 4. 使用VS 2013社区sp 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM