2009-04-21 75 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是真實的資源。當控件崩潰時,我希望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

工作很好!見上面的解決方案:) – Pwninstein 2009-04-21 13:43:28

相關問題