2017-08-28 11 views
1

次のMultiBinding式の場合、PropBが複数回変更された場合にバインディングエンジンがDataGrid祖先を何回検索しますか?WPF Multibinding RelativeSource Findancestor評価パフォーマンス

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

PropertyC(およびそのパス)が変更されていない場合は一度だけ検索しますか?または、マルチバインディング内のプロパティの1つが変更されるたびに祖先を検索しますか?各プロパティに変更通知があると仮定します。

答えて

2

これをテストするには、コントロールを実際に削除して、適切なコントロールが見つかったかどうかを確認するしかありません。

このようにテストすると、MultiBindingを使用しているかどうかを1回だけ評価されているように見えます。

enter image description here

ボタンをクリックすると、それは最初の子を削除し、それが今で結合する1つを示すために、新しいテキストを表示するには、テキストブロックを変更します:最初にそれを実行しているときに表示されます

<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

+0

本当にありがとうございました。 – Yoghurt