4

次のコントロール/テンプレートを検討してください

<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に表示する方法はありますか?

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

編集(解決策):

<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」は、表示しているアイテムのタイプの既知のプロパティです。

4

1 に答える 1

3

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

<TextBlock Name="MySelectionInfo" Background="White"
    Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:ExpandingListBox}}}" />
于 2009-04-21T13:23:46.767 に答える