WPF 相对源行为

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

WPF RelativeSource behavior

wpfdata-bindingbinding

提问by WpfBee

I have some problems in understanding RelativeSourcebinding behavior. Below is the code that binds Labelcontent to StackPanelTag correctly:

我在理解RelativeSource绑定行为方面遇到了一些问题。下面是将Label内容StackPanel正确绑定到标签的代码:

<Window x:Class="Binding_RelativeSource.MainWindow" Tag="Window Tag">
    <Grid Tag="Grid Tag">
        <StackPanel Tag="StackPanel Tag" Height="100" HorizontalAlignment="Left" Margin="156,97,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="200">
            <Label Content="{Binding Path=Tag,RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType=StackPanel},FallbackValue=BindingFailed}" Height="28" Name="label1" />
        </StackPanel>
    </Grid>
</Window>

Above code does not bind GridTag, if I change AncestorType=Gridand AncestorLevel=2. I have 2 questions:

上面的代码不绑定Grid标签,如果我改变AncestorType=GridAncestorLevel=2。我有两个问题:

  1. I think I should change AncestorLevel to 2, to bind to Grid. But it worked for AncestorLevel=1. Why?

  2. I am also not able to bind label to Window tag.Please suggest.

  1. 我想我应该将 AncestorLevel 更改为 2,以绑定到 Grid。但它对AncestorLevel=1. 为什么?

  2. 我也无法将标签绑定到 Window 标签。请建议。

采纳答案by WpfBee

Final conclusion from my end: It was VS2010 designer problem that it does not update RelativeSource binding for Window tag. It updates binding for other controls (I checked with Grid & StackPanel) in designer but for Winodw it gets updated at run time. Microsoft has done workaround in VS2012 for it.

我最后得出的结论是:VS2010 设计器的问题是它没有更新 Window 标签的 RelativeSource 绑定。它在设计器中更新了其他控件的绑定(我使用 Grid 和 StackPanel 进行了检查),但对于 Winodw,它会在运行时更新。微软已经在 VS2012 中为它做了解决方法。

回答by sa_ddam213

The AncestorLevelis use to find the correct ancestor to bind to, this is because there could be more than one ancestor of that type.

AncestorLevel是利用找到正确的祖先绑定到,这是因为有可能是该类型的不止一个祖先。

Here is a scenario that shows this:

这是一个场景,显示了这一点:

<Grid Tag="AncestorLevel 3">
    <Grid Tag="AncestorLevel 2">
        <Grid Tag="AncestorLevel 1">
            <StackPanel Tag="StackPanel Tag" Height="100" HorizontalAlignment="Left" Margin="156,97,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="200">
                <Label Content="{Binding Path=Tag,RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType=Grid},FallbackValue=BindingFailed}" Height="28"  />
                <Label Content="{Binding Path=Tag,RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=2,AncestorType=Grid},FallbackValue=BindingFailed}" Height="28"  />
                <Label Content="{Binding Path=Tag,RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=3,AncestorType=Grid},FallbackValue=BindingFailed}" Height="28"  />
            </StackPanel>
        </Grid>
    </Grid>
</Grid>

Result:

结果:

enter image description here

在此处输入图片说明

Alternative Method

替代方法

But you can simplify the code by using ElementNamebinding, this uses the Nameof the element

但是您可以通过使用ElementName绑定来简化代码,这使用Name了元素的

Example:

例子:

<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="MyWindow" Tag="This is the window">
    <Grid Name="Grid1" Tag="First grid">
        <Grid Name="Grid2" Tag="Second grid">
            <Grid Name="Grid3" Tag="ThirdGrid">
                <StackPanel Name="stackPanel1" Tag="StackPanel Tag" Height="160" HorizontalAlignment="Left" Margin="156,97,0,0" VerticalAlignment="Top" Width="200">
                    <Label Content="{Binding ElementName=MyWindow, Path=Tag}" Height="28"  />
                    <Label Content="{Binding ElementName=Grid1, Path=Tag}" Height="28"  />
                    <Label Content="{Binding ElementName=Grid2, Path=Tag}" Height="28"  />
                    <Label Content="{Binding ElementName=Grid3, Path=Tag}" Height="28"  />
                    <Label Content="{Binding ElementName=stackPanel1, Path=Tag}" Height="28"  />
                </StackPanel>
            </Grid>
        </Grid>
    </Grid>
</Window>

Result:

结果:

enter image description here

在此处输入图片说明

If you want to bind back to the Windowyou can still use FindAncestor

如果你想绑定回Window你仍然可以使用FindAncestor

<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Tag="This is the window">
    <Grid>
        <StackPanel Height="100" HorizontalAlignment="Left" Margin="156,97,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="200">
            <Label Content="{Binding Path=Tag,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window},FallbackValue=BindingFailed}" Height="28"  />
        </StackPanel>
    </Grid>

Result:

结果:

enter image description here

在此处输入图片说明