为什么以下xaml代码不起作用?

时间:2011-06-21 11:32:01

标签: xaml

为什么这个xaml代码不起作用?

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
  <Grid.Resources>
    <ControlTemplate x:Key="btnTemplate" TargetType="{x:Type Button}">
      <Grid>
       <Ellipse Name="el1" Fill="Orange" Width="100" Height="100" />
       <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" /> 
      </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="Button.IsMouseOver" Value="True">
        <Setter TargetName="el1" Property="Background" Value="Blue" />
      </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
  </Grid.Resources>
  <Button Content="Klick mich" Template="{StaticResource btnTemplate}"/>
  </Grid>
</Page>

1 个答案:

答案 0 :(得分:1)

您正尝试在Trigger中设置Background属性,但Ellipse没有Background属性。它有Fill属性。所以你需要使用:

<Grid>
    <Grid.Resources>
        <ControlTemplate x:Key="btnTemplate" TargetType="{x:Type Button}">
            <Grid>
                <Ellipse Name="el1" Fill="Orange" Width="100" Height="100" />
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="Button.IsMouseOver" Value="True">
                    <Setter TargetName="el1" Property="Fill" Value="Blue" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Grid.Resources>
    <Button Content="Klick mich" Template="{StaticResource btnTemplate}" />
</Grid>