为什么控件模板中的样式不能使用templatebinding?

时间:2010-04-25 20:26:12

标签: wpf styles controltemplate

我正在编写这个控件模板:

<Style TargetType="{x:Type controls:InfoBar}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:InfoBar}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel>
                        <Grid>
                            <Grid.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="FontFamily" Value="{TemplateBinding FontFamily}" />
                                    <Setter Property="FontSize" Value="{TemplateBinding FontSize}" />
                                    <Setter Property="Foreground" Value="{TemplateBinding Foreground}" />
                                </Style>
                            </Grid.Resources>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <ItemsControl Grid.Column="0" ItemsSource="{TemplateBinding LeftInfoBarTextBlockCollection}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                            <ItemsControl Grid.Column="1" ItemsSource="{TemplateBinding MiddleInfoBarTextBlockCollection}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                            <ItemsControl Grid.Column="2" ItemsSource="{TemplateBinding RightInfoBarTextBlockCollection}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                        </Grid>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

xaml的这一部分为FontFamily,FontSize和Foreground上的模板绑定抛出了member is not valid because it does not contain a valid type name.个异常。

<Grid.Resources>

                               

如果我将其更改为:

 <Grid.Resources>

                               

它会构建,但是当我调试它时,我得到了这个XmlParseExeption

Set property 'System.Windows.Setter.Value' threw an exception.

如果我将controls:InfoBar更改为Control,InfoBar会从中继承,我会得到相同的例外。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

问题在于:

<Style TargetType="{x:Type TextBlock}">         
   <Setter Property="FontFamily" Value="{TemplateBinding FontFamily}" />         
   <Setter Property="FontSize" Value="{TemplateBinding FontSize}" />         
   <Setter Property="Foreground" Value="{TemplateBinding Foreground}" />         
</Style>

您只能在控件模板中使用 TemplateBinding 。 在这里你在一个风格中使用它。

答案 1 :(得分:1)

要回答第二个问题,Justin,您可以创建一个应用控件模板的样式。

请注意,当图钉样式设置为NumberPushpinStyle时,此样式会将控件模板分配给图钉的Template属性。

<Style x:Key="NumberPushpinStyle" TargetType="m:Pushpin">
  <Setter Property="BorderBrush" Value="#FFF4F4F5" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate>
        <Grid>
          <Ellipse Fill="Black" Height="33" Stroke="White" StrokeThickness="2" Width="33" RenderTransformOrigin="0.5,0.5">
            <Ellipse.RenderTransform>
              <CompositeTransform TranslateX="-16" TranslateY="16" />
            </Ellipse.RenderTransform>
          </Ellipse>
          <TextBlock Foreground="White" Text="{TemplateBinding Content}" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0">
            <TextBlock.RenderTransform>
              <CompositeTransform TranslateX="-16" TranslateY="15" />
            </TextBlock.RenderTransform>
          </TextBlock>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>