gpt4 book ai didi

WPF 相对源行为

转载 作者:行者123 更新时间:2023-12-03 18:10:09 27 4
gpt4 key购买 nike

我在理解上有些问题 RelativeSource绑定(bind)行为。
下面是绑定(bind)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>

以上代码不绑定(bind) Grid标记,如果我更改 AncestorType=GridAncestorLevel=2 .
我有两个问题:
  • 我想我应该将 AncestorLevel 更改为 2,以绑定(bind)到 Grid。但它
    AncestorLevel=1工作.为什么?
  • 我也无法将标签绑定(bind)到窗口标签。请建议。
  • 最佳答案

    AncestorLevel用于查找要绑定(bind)的正确祖先,这是因为该类型的祖先可能不止一个。

    这是一个显示这一点的场景:

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

    结果:

    enter image description here

    替代方法

    但是你可以使用 ElementName 来简化代码。绑定(bind),这使用 Name元素的

    例子:
    <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>

    结果:

    enter image description here

    如果要绑定(bind)回 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>

    结果:

    enter image description here

    关于WPF 相对源行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15237037/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com