0

我正在做一个HeaderedContentControl基本上是这样风格的自定义:

<Style TargetType="{x:Type controls:DetailsControl}">
    <Style.Resources>
        <!-- Only if content is string provide a default Template -->
        <DataTemplate DataType="{x:Type system:String}">
            <TextBlock Text="{TemplateBinding Content}"
                       FontSize="{DynamicResource NormalFontSize}" />
        </DataTemplate>
    </Style.Resources>
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate DataType="{x:Type system:String}">
                <TextBlock Text="{TemplateBinding Content}"
                           FontWeight="Light"
                           FontSize="{DynamicResource SubHeaderFontSize}"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template" Value="{StaticResource DetailsControlTemplate}" />
</Style>

<ControlTemplate x:Key="DetailsControlTemplate"
                 TargetType="{x:Type controls:DetailsControl}">
    <StackPanel>
        <ContentPresenter ContentSource="Header"
                          ContentTemplate="{TemplateBinding HeaderTemplate}"/>

        <ContentPresenter ContentSource="Content"
                          ContentTemplate="{TemplateBinding ContentTemplate}"/>
    </StackPanel>
</ControlTemplate>

当我只是将 Header 设置为字符串时,这很好用

<controls:DetailsControl Header="Header Text" [...]>
    [...]
</controls:DetailsControl>

或普通绑定

<controls:DetailsControl Header="{Binding SelectedItem.Name, ElementName=ElementList}" [...]>
    [...]
</controls:DetailsControl>

但是当我将 Header 设置为 Multibinding 时,它根本不会显示任何内容。

<controls:DetailsControl [...]>
    <controls:DetailsControl.Header>
        <MultiBinding StringFormat="{}{0} - {1}">
            <Binding ElementName="ElementList" Path="SelectedItem.Name"/>
            <Binding ElementName="ElementList" Path="SelectedItem.Description"/>
        </MultiBinding>
    </controls:DetailsControl.Header>

    [...]
</controls:DetailsControl>

我在哪里做错了?解决此问题的正确方法是什么?

4

1 回答 1

1

StringFormat要求目标是一种string类型。由于Header是内容字段,请尝试将其放入TextBlock

<controls:DetailsControl.Header>
   <TextBlock>
      <TextBlock.Text>
         <Multibinding .../>
      </TextBlock.Text>
   </TextBlock>
</controls:DetailsControl.Header>
于 2013-06-17T08:39:13.297 回答