本文介绍了如何访问X:名称,属性code - 非FrameworkElement的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个问题类似,我想进入X:通过code对象的名称属性,强悍在这种情况下所讨论的对象不是 FrameworkElement的,因此不具有Name属性。我没有访问成员变量任一。

Similar to another question, I would like to access the x:Name property of an object through code, tough in this case the object in question is not a FrameworkElement and thus does not have a Name property. I do not have access to the member variable either.

在我的情况,我有一个的ListView 与命名的列,并希望延长的ListView类,以便它继续存在的列布局。对于这个功能,我需要命名的列,并且将是有意义的我重新使用x:我需要无论如何设定,而不是增加一个附加的的ColumnName的属性,例如其他原因,Name属性

In my situation, I have a ListView with named columns and would like to extend the ListView class so that it persists the column layout. For this functionality I need named columns, and it would make sense to me to re-use the x:Name property I need to set anyway for other reasons, instead of adding an attached "ColumnName" property for example.

我目前的解决方案:

<GridViewColumn Header="X" localControls:ExtendedListView.ColumnName="iconColumn" />

期望:

<GridViewColumn Header="X" x:Name="iconColumn" />

那么,有可能得到的X:名称?值在某种程度上

So is it possible to get the "x:Name" value somehow?

推荐答案

请参阅以下螺纹的答案被IANG:
How阅读X:从XAML code Name属性在code-背后

See the answers by IanG in the following thread:
How to read the x:Name property from XAML code in the code-behind

不幸的是,这并不完全是X:名称一样。 X:名称不  技术上一个属性 - 这是一个XAML中的指令。在一个范围内  .NET WPF应用程序,X:名字的意思是这样的:

我要通过这一个字段,以便能够访问这个对象  名称。此外,如果这种类型恰好有一个名称属性,请设置  它的名字。

"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."

这是你需要的第二部分,不幸的是这第二  部分不适用于ModelUIElement3D,因为这种类型的不具有  一个属性重新present的名称。所以实际上,所有它的意思是我  要通过这一个字段,以便能够访问这个对象  名字。而这一切。

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:名称确实是可以让您通过与特定的名称创建一个字段访问该对象。如果你想获得的X:它的名字,你就必须遍历所有的领域,看看现场的是你要寻找的对象,在这种情况下,返回的字段名

So, all x:Name does is that it gives you access to this object by creating a field with that specific name. If you want to get the x:Name of it, you'll have to iterate all your fields and see if the field is the object you're looking for, and in that case, return the field name.

他做present的方法来做到这一点在后面的文件中的code,虽然我觉得你现在的做法带有附加属性是一个更好的解决方案。

He does present a method to do this in the code behind file, although I think your current approach with an attached property is a much better solution

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();
}

这篇关于如何访问X:名称,属性code - 非FrameworkElement的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:33