gpt4 book ai didi

c# - WPF Multibinding RelativeSource Findancestor 评估性能

转载 作者:行者123 更新时间:2023-11-30 23:09:16 28 4
gpt4 key购买 nike

对于以下 MultiBinding 表达式,如果多次更改 PropB,绑定(bind)引擎将搜索 DataGrid 祖先多少次?

<MultiBinding Converter="{StaticResource TestConverter}"> 
<Binding Path="PropA"/>
<Binding Path="PropB" />
<Binding Path="DataContext.Sub.PropertyC" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=DataGrid}" />
</MultiBinding>

如果 PropertyC(及其路径)从未改变,它是否只搜索一次?还是每次更改多重绑定(bind)中的一个属性时它都会搜索祖先?假设每个属性都有更改通知。

最佳答案

我认为唯一可行的测试方法是实际删除控件,以查看它是否找到了正确的控件。

以这种方式进行测试,无论是否使用 MultiBinding,它看起来都只被评估一次。

<Window x:Class="RelativeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="Stack">
<TextBlock x:Name="TB1" Text="Foo" />
<TextBlock x:Name="TB2" Text="Bar" />

<Border BorderThickness="1" BorderBrush="Black" />

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=StackPanel}, Path=Children[0].Text}"
Foreground="Red" />

<TextBlock Foreground="Blue">
<TextBlock.Text>
<MultiBinding StringFormat="{}{2}">
<Binding ElementName="TB1" Path="Text" />
<Binding ElementName="TB2" Path="Text" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=StackPanel}" Path="Children[0].Text" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<Button Click="ButtonBase_OnClick" Content="Remove 1st Child" />
</StackPanel>
</Window>

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Stack.Children.Remove(Stack.Children[0]);
TB1.Text = "You'll see me if I am looked up once.";
TB2.Text = "You'll see me twice if I am re-evaulated each time";
}
}

最初运行它时你会看到:

enter image description here

单击按钮时,它将删除第一个子项并更改 TextBlocks 以显示更新的文本以指示它现在绑定(bind)到哪个子项。

enter image description here

关于c# - WPF Multibinding RelativeSource Findancestor 评估性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45927423/

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