wpf 绑定控件的 x:Name

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19225437/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:47:58  来源:igfitidea点击:

Binding a control's x:Name

c#wpfxamlbinding

提问by struggleendlessly

When I add

当我添加

    <TextBlock  Text="{Binding SettingName}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />    

Everuthing is ok. But when

万事如意。但当

 <TextBlock x:Name="{Binding SettingTextBlockName}"  Text="{Binding SettingName}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />

constructor are breaking.

构造函数正在破坏。

But I need different names in all elements.

但是我需要在所有元素中使用不同的名称。

回答by CKII

x:Nameis a special property. As a matter of fact it's not a property at all, it's an attribute that maps the name or id property of the element to x:Name. Binding only works when applied to a DependencyProperty, so it cannot work on x:Name. It must be set manually.

x:Name是一个特殊的属性。事实上,它根本不是一个属性,它是一个将元素的 name 或 id 属性映射到x:Name. 绑定仅在应用于 a 时才有效DependencyProperty,因此无法在x:Name. 必须手动设置。

If you want to distinguish between objects in runtime, you can set the Tagattribute, which tolerates everything.

如果你想在运行时区分对象,你可以设置Tag属性,它容忍一切。

more on x:Name: http://msdn.microsoft.com/en-us/library/ms752290.aspx

更多信息x:Namehttp: //msdn.microsoft.com/en-us/library/ms752290.aspx

回答by Xyroid

You should use FrameworkElement.Tagproperty, according to MSDN

FrameworkElement.Tag根据 MSDN,您应该使用财产

FrameworkElement.Tag gets or sets an arbitrary object value that can be used to store custom information about this element.

FrameworkElement.Tag 获取或设置可用于存储有关此元素的自定义信息的任意对象值。

What use is the Tag property in .net

.net 中的 Tag 属性有什么用