c# – 如何访问x:代码中的Name-property – 对于非FrameworkElement对象?

c# – 如何访问x:代码中的Name-property – 对于非FrameworkElement对象?,第1张

概述与 another question类似,我想通过代码访问对象的x:Name属性,在这种情况下,有问题的对象不是FrameworkElement,因此没有Name属性.我也无法访问成员变量. 在我的情况下,我有一个带有命名列的ListView,并希望扩展ListView类,以便它保持列布局.对于这个功能,我需要命名列,因为其他原因重新使用我需要设置的x:Name属性是有意义的,而不是添加附加的“C 与 another question类似,我想通过代码访问对象的x:name属性,在这种情况下,有问题的对象不是FrameworkElement,因此没有name属性.我也无法访问成员变量.

在我的情况下,我有一个带有命名列的ListVIEw,并希望扩展ListVIEw类,以便它保持列布局.对于这个功能,我需要命名列,因为其他原因重新使用我需要设置的x:name属性是有意义的,而不是添加附加的“Columnname”属性.

我目前的“解决方案”:

<GrIDVIEwColumn header="X" localControls:ExtendedListVIEw.Columnname="iconColumn" />

期望:

<GrIDVIEwColumn header="X" x:name="iconColumn" />

那么有可能以某种方式获得“x:name”值吗?

解决方法 请参阅IanG在以下主题中的答案:
How to read the x:Name property from XAML code in the code-behind

Unfortunately,that’s not quite what x:name does. x:name isn’t
technically a property – it’s a Xaml directive. In the context of a
.NET WPF application,x:name means this:

“I want to be able to have access to this object via a fIEld of this
name. Also,if this type happens to have a name property,please set
it to that name.”

It’s that second part that you need,and unfortunately that second
part doesn’t apply to ModelUIElement3D,because that type doesn’t have
a property to represent the name. So in effect,all it means is “I
want to be able to have access to this object via a fIEld of this
name.” And that’s all.

因此,所有x:name都可以通过创建具有该特定名称的字段来访问此对象.如果要获取x:它的名称,则必须迭代所有字段并查看该字段是否是您要查找的对象,返回字段名称.

他确实提出了一种在代码隐藏文件中执行此 *** 作的方法,尽管我认为您当前使用附加属性的方法是一个更好的解决方案

public string Getname(GrIDVIEwColumn column){    var findMatch = from fIEld in this.GetType().GetFIElds(BindingFlags.NonPublic | BindingFlags.Instance)                    let fIEldValue = fIEld.GetValue(this)                    where fIEldValue == column                    select fIEld.name;    return findMatch.FirstOrDefault();}
总结

以上是内存溢出为你收集整理的c# – 如何访问x:代码中的Name-property – 对于非FrameworkElement对象?全部内容,希望文章能够帮你解决c# – 如何访问x:代码中的Name-property – 对于非FrameworkElement对象?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1247328.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存