2009-04-21 15 views
2

ControlTemplateのSelectedItemプロパティにどのようにバインドするのですか?

<my:ExpandingListBox Margin="0,2,0,0" x:Name="TagSearchListBox"> 
    <my:ExpandingListBox.Template> 
     <ControlTemplate TargetType="{x:Type my:ExpandingListBox}"> 
      <Border Name="MyBorder" BorderThickness="2" BorderBrush="Black"> 
       <Grid> 
        <TextBlock Name="MySelectionInfo" Background="White" Text="{TemplateBinding SelectedItem}"/> 
        <ScrollViewer Name="MyScrollViewer" HorizontalScrollBarVisibility="Hidden" Opacity="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Hidden"> 
         <ItemsPresenter Name="MyItemsPresenter"/> 
        </ScrollViewer> 
       </Grid> 
      </Border> 
     </ControlTemplate> 
    </my:ExpandingListBox.Template> 
</my:ExpandingListBox> 

は、基本的には、追加のトリガ/ IsMouseOverがtrueの場合/崩壊を拡大する制御を引き起こすリソースがある以下のコントロール/テンプレートに考えてみましょう。コントロールが折りたたまれているとき、私はTextBlock "MySelectionInfo"に選択した項目のテキストを表示したいと思います。それが展開されると、アイテムのリストが通常のように表示されるようにしたいと思います。選択したアイテムのテキストを取得し、それを純粋なXAMLのTextBlockに表示する方法はありますか?

設定テキスト= "{TemplateBinding SelectedItem}"は実行されますが、何も表示されません。

EDIT(ソリューション):

<TextBlock Name="MySelectionInfo" Background="White"> 
    <TextBlock.Text> 
     <Binding Path="SelectedItem.Name"> 
      <Binding.RelativeSource> 
       <RelativeSource Mode="FindAncestor" AncestorType="{x:Type my:ExpandingListBox}"/> 
      </Binding.RelativeSource> 
     </Binding> 
    </TextBlock.Text> 
</TextBlock> 

".nameのは、" 私が表示されていた項目の種類の既知の特性です。

答えて

2

代わりにRelativeSourceを使用してバインドできますか?多分このようなもの:

<TextBlock Name="MySelectionInfo" Background="White" 
    Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:ExpandingListBox}}}" /> 
+0

Worked great!ソリューションについては上記を参照してください:) – Pwninstein

関連する問題