是否有人注意到带有ElementName的Bindings无法正确解析MenuItem
对象中包含的ContextMenu
对象?看看这个样本:
<Window x:Class="EmptyWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
x:Name="window">
<Grid x:Name="grid" Background="Wheat">
<Grid.ContextMenu>
<ContextMenu x:Name="menu">
<MenuItem x:Name="menuItem" Header="Window" Tag="{Binding ElementName=window}" Click="MenuItem_Click"/>
<MenuItem Header="Grid" Tag="{Binding ElementName=grid}" Click="MenuItem_Click"/>
<MenuItem Header="Menu" Tag="{Binding ElementName=menu}" Click="MenuItem_Click"/>
<MenuItem Header="Menu Item" Tag="{Binding ElementName=menuItem}" Click="MenuItem_Click"/>
</ContextMenu>
</Grid.ContextMenu>
<Button Content="Menu"
HorizontalAlignment="Center" VerticalAlignment="Center"
Click="MenuItem_Click" Tag="{Binding ElementName=menu}"/>
<Menu HorizontalAlignment="Center" VerticalAlignment="Bottom">
<MenuItem x:Name="anotherMenuItem" Header="Window" Tag="{Binding ElementName=window}" Click="MenuItem_Click"/>
<MenuItem Header="Grid" Tag="{Binding ElementName=grid}" Click="MenuItem_Click"/>
<MenuItem Header="Menu" Tag="{Binding ElementName=menu}" Click="MenuItem_Click"/>
<MenuItem Header="Menu Item" Tag="{Binding ElementName=anotherMenuItem}" Click="MenuItem_Click"/>
</Menu>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
除了ContextMenu中包含的绑定之外,所有绑定都很有效.它们在运行时将错误输出到"输出"窗口.
有谁知道任何工作?这里发生了什么?
如果我使用XPATH选择了一个元素,我怎么能得到它的名字?
我的意思是像text()
函数一样//element/[@id=elid]/text()
.
有{Binding ElementName=foo}
,它会查找视觉或逻辑树吗?
逻辑树什么时候重要?
当查找名称时,例如在{Binding ElementName = Foo}中,搜索会向祖先寻找名称范围,就像对可继承属性一样.
Silverlight中的ElementName绑定通过附加行为
为了实现这一点,WPF提供了ElementName和RelativeSource绑定,为您提供了一种强大的机制,用于定位可视树中要绑定的其他元素.
编辑:
它看起来像用于绑定的逻辑树ElementName
.
争论#1.
根据MSDN文章FrameworkElement类:
FrameworkElement扩展了UIElement并添加了以下功能:
- 支持数据绑定和动态资源引用:对数据绑定和资源的属性级支持由DependencyProperty类实现并体现在属性系统中,但是能够解析存储为Expression的成员值(编程构造) FrameworkElement实现了数据绑定和动态资源的基础.有关更多信息,请参阅数据绑定概述和资源概述.
争论#2.
ElementName
指向x:Name
,所以这个名字应该找到一些如何.有一个NameScope概念.
对于大多数情况,在FrameworkElement和FrameworkContentElement上公开的FindName方法是调用按名称搜索元素的更合适的方法.FrameworkElement和FrameworkContentElement公开的Name属性是用于将初始名称设置为标记属性的更合适的属性.并且在FrameworkElement和FrameworkContentElement上公开的RegisterName方法对于在特定的名称范围内建立名称是必要的(没有可以直接执行此操作的NameScope成员;您必须首先将当前的namescope设置为使用RegisterName).
另一方面,Visual类既没有FindName
方法也没有实现INameScope.
以下哪些TextBlocks的绑定会降低性能:
<Window
x:Name="Me"
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication1"
Title="MainWindow">
<StackPanel>
<TextBlock Text="{Binding Title, ElementName=Me}"/>
<TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType={x:Type src:MainWindow}}}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
我确信当TextBlocks处于具有许多兄弟和祖先的高嵌套级别时,我的问题可能会有所不同.
(仅基于个人想法,我可能在每个特定的人中都错了!):
ElementName
:
Name
控件的属性应该比调用成本低GetType
.Name
设置时.FindAncestor
:
GetType
确定祖先类型; GetType比简单的Name
属性获取器(可能DP不同?)的性能更高通过名称进行绑定自引用与通过RelativeSource进行自引用之间有什么区别?
例如:
<!-- Self referential via name -->
<Window ...
x:Name="This"
DataContext="{Binding Path=Data, ElementName=This}"/>
Run Code Online (Sandbox Code Playgroud)
与
<!-- Self referential via RelativeSource -->
<Window ...
x:Name="This"
DataContext="{Binding Path=Data, RelativeSource={RelativeSource Self}}"/>
Run Code Online (Sandbox Code Playgroud)
在我的项目中,它们看起来表现相同,但我在窗口中直接使用它们.这意味着我立刻更喜欢前者,因为它输入较少.
RelativeSource的唯一优势是它能够在(例如)广泛使用的风格中自我引用吗?还是有一些额外的语义差异?
可能重复:
jQuery可以提供标签名称吗?
嗨!
这个问题是如此基本我感到惭愧,但我试图找到答案30分钟没有任何结果.
我如何找到下面代码中点击了哪种元素.
$('*').click(function (event) {
var this_element = $(this).???;
return false;
})
Run Code Online (Sandbox Code Playgroud)
我想要的是将this_element变量设置为'a'如果它是一个链接,'p'如果它是一个段'div'如果...
谢谢!
我想向向量 V 添加一个元素,比如 100,并使用变量 x 的值作为新元素的名称。我知道可以这样做:
V = c(V, 100)
names(V)[length(V)] = x
Run Code Online (Sandbox Code Playgroud)
但我正在寻找一种简单的单线解决方案,如果有的话。我试过:
V = c(V, as.name(x)=100)
Run Code Online (Sandbox Code Playgroud)
和
V = c(V, eval(x)=100)
Run Code Online (Sandbox Code Playgroud)
但那些不起作用。
好的,发现了最好的方法:
V[x] = 100
Run Code Online (Sandbox Code Playgroud) 下面代码的问题是:绑定到SomeClassProp.SubTextProp
不起作用(源属性未设置为文本框内容),而绑定到TextProp
它。
XAML:
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Name="wMain"
SizeToContent="WidthAndHeight">
<StackPanel>
<TextBox Text="{Binding ElementName=wMain, Path=SomeClassProp.SubTextProp}" Width="120" Height="23" />
<TextBox Text="{Binding ElementName=wMain, Path=TextProp}" Width="120" Height="23" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
和代码:
public partial class MainWindow : Window
{
public SomeClass SomeClassProp { get; set; }
public string TextProp { get; set; }
public MainWindow()
{
InitializeComponent();
SomeClassProp = new SomeClass();
}
}
public class SomeClass
{
public string SubTextProp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在这里遗漏了一些明显的东西吗?
请注意,我需要此绑定才能从目标(文本框)到源(类属性)工作。
更新:当我将绑定更改ElementName=wMain
为RelativeSource={RelativeSource …
elementname ×8
wpf ×5
data-binding ×3
binding ×2
c# ×1
contextmenu ×1
jquery ×1
logical-tree ×1
performance ×1
r ×1
visual-tree ×1
xaml ×1
xpath ×1