wpf 为什么在代码隐藏中无法访问我的控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12231743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my control not accessible in codebehind?
提问by NDeveloper
When I create a WPF application in VS 2010 and place a button in Grid I can access the control from C# code. I have the button class instance variable with the same name as in XAML "name" field. But I can't see the instance of the control when I create a WPF project using VS 2012 RC. What can be the problem ? Is there any default project settings difference between VS 2010 and 2012 ?
当我在 VS 2010 中创建 WPF 应用程序并在 Grid 中放置一个按钮时,我可以从 C# 代码访问控件。我有与 XAML“名称”字段同名的按钮类实例变量。但是当我使用VS 2012 RC创建WPF项目时,我看不到控件的实例。可能是什么问题?VS 2010 和 2012 之间是否有任何默认项目设置差异?
回答by Erre Efe
If you're talking about codebehind then you should be able to access to the button withoutdeclaring it again just by using it's x:nameattribute. Now, if you're talking about MVVM, then you should set your datacontext before using the button.
如果您在谈论代码隐藏,那么您应该能够访问该按钮而无需再次声明它,只需使用它的x:name属性即可。现在,如果您在谈论 MVVM,那么您应该在使用按钮之前设置您的数据上下文。
When I'm using 2012 (Release Candidate) I get no intellisense for the codebehind cs files when trying to access elements declared in the xaml (I have to wrote the code without it) and it even suggest there's no element by that name but project compiles and works. So I'll suggest you to try to write the code without intellisense and compile it.
当我使用 2012 (Release Candidate) 时,当我尝试访问在 xaml 中声明的元素时,我没有得到代码隐藏 cs 文件的智能感知(我必须在没有它的情况下编写代码),它甚至表明没有该名称的元素,但项目编译和工作。所以我建议你尝试在没有智能感知的情况下编写代码并编译它。
YourButton.IsEnabled = false;
I guess this is by default in VS 2012 to encourage use of MVVM but that's no more than thoughts.
我猜这是 VS 2012 中的默认设置,以鼓励使用 MVVM,但这只是想法。
回答by William
You need to be sure that you are not accessing the control in a static method.
您需要确保您不是在静态方法中访问控件。
回答by Swamitra Singh
I have another experience with the same Problem.
我有同样的问题的另一种经验。
When I tried accessing the element from code behind, there was no intellisense showing it and I wasn't able to use methods with it. So I tried to debug the project and I found that there was a problem that was logical and was not a syntax error so it was not showing in XAML code or anywhere. I fixed it and then I was able to access it from code behind. Make sure the elements you are trying to access is correctly defined and has properties that are logically correct.
当我尝试从后面的代码访问元素时,没有智能感知显示它,我无法使用它的方法。所以我尝试调试该项目,我发现存在一个符合逻辑的问题,而不是语法错误,因此它没有显示在 XAML 代码或任何地方。我修复了它,然后我可以从后面的代码访问它。确保您尝试访问的元素被正确定义并且具有逻辑上正确的属性。
<ComboBox x:Name="_days" Selected="0" Width="50"/>
this was the code and Selected property was logically incorrect cause there was no element in combobox and you cant select 0th element.
这是代码,Selected 属性在逻辑上不正确,因为组合框中没有元素,您无法选择第 0 个元素。

