2017-03-07 20 views
0

私は、次のツリービューを持っている:DataTemplateの兄弟要素にバインドする方法は?

<TreeView Grid.Column="2" x:Name="itemTreeView" Margin="0" SelectedItemChanged="SelectionChanged"> 
    <TreeView.Resources> 
    <HierarchicalDataTemplate DataType="{x:Type local:MarketGroup}"> 
     <HierarchicalDataTemplate.ItemsSource> 
     <Binding Path="children"/> 
     </HierarchicalDataTemplate.ItemsSource> 
     <TextBlock Text="{Binding marketGroupName}" ToolTip="{Binding description}"/> 
    </HierarchicalDataTemplate> 
    <DataTemplate DataType="{x:Type local:ItemType}"> 
     <StackPanel Orientation="Horizontal"> 
     <Image Source="{Binding itemImage}" Height="{Binding Sibling.ActualHeight}"/> 
     <TextBlock Text="{Binding typeName}" ToolTip="{Binding description}"/> 
     </StackPanel> 
    </DataTemplate> 
    </TreeView.Resources> 
</TreeView> 

それでは、私がやりたいことは、彼らが同じサイズになるようDataTemplateをバインドに位置画像は、それはその横のTextBlockの実際の高さまでの高さだ持っています。これまでのところ、兄弟を参照するために頼りにしていたのは、x:Nameを定義して名前で参照することに依存していましたが、それは唯一のテンプレートなのでDataTemplateの要素に名前を付けることはできません。代わりに、親StackPanelをTextBlockにバインドする方法があった場合、親にImageサイズをバインドすることも可能です。

答えて

2

x:Name(TextBlock)、ElementName(バインディング)。

<Image 
    Source="{Binding itemImage}" 
    Height="{Binding ActualHeight, ElementName=SiblingTextBlock}" 
    /> 
<TextBlock 
    x:Name="SiblingTextBlock" 
    Text="{Binding typeName}" 
    ToolTip="{Binding description}" 
    /> 

DataTemplateでうまくいかないとお伝えしたのは誰ですか?あなたは当然の外部からその名前を参照することができませんテンプレートはインスタンス化されていない可能性があります。しかしテンプレート内では問題ありません。

+1

おそらく、私はそれをテストした 'DataTemplate'の外からの参照を参照していて、私が探していたことをやっています。私はちょうどそれをとにかく試みるべきだったと思う。 – themightymanuel

+0

@themightymanuel私はそれがあったと思います。多くの人々がスコープに入ります。 –

関連する問題