none
WPF写 controltemplate的一个问题 RRS feed

  • 问题

  •     <ControlTemplate x:Key="ComboBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">
          <Grid>
            <Grid.Background>
              <ImageBrush ImageSource="btn3.png"/>
            </Grid.Background>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsMouseOver" Value="True">
              <Setter Property="Grid.Background">
                <Setter.Value>
                  <ImageBrush ImageSource="btn3_hover.png"/>
                </Setter.Value>
              </Setter>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
    
    我想让这个ToggleButton在mouseover状态下改变他的背景,这样写却不管用,不知道有没有人可以指点一下~先谢谢了~
    2011年6月3日 2:37

答案

  • 你应该用Style去设置一个属性值作为默认值,然后当触发器离开的时候才会恢复到默认值。

        <Style x:Key="ComboBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">
          <Setter Property="Background">
            <Setter.Value>
              <ImageBrush ImageSource="..."/>
            </Setter.Value>
          </Setter>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Grid Background="{TemplateBinding Background}">
                </Grid>
                <ControlTemplate.Triggers>
                  <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background">
                      <Setter.Value>
                        <ImageBrush ImageSource="..."/>
                      </Setter.Value>
                    </Setter>
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>

     

    或者在ControlTemplate里面的触发器用 TargetName 来指定更改某个元素:

        <ControlTemplate x:Key="ComboBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">
          <Grid x:Name="grid">
            <Grid.Background>
              <ImageBrush ImageSource="..."/>
            </Grid.Background>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsMouseOver" Value="True">
              <Setter TargetName="grid" Property="Background">
                <Setter.Value>
                  <ImageBrush ImageSource="..."/>
                </Setter.Value>
              </Setter>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Colinfc 2011年6月3日 9:15
    2011年6月3日 6:20
    版主

全部回复

  • 你应该用Style去设置一个属性值作为默认值,然后当触发器离开的时候才会恢复到默认值。

        <Style x:Key="ComboBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">
          <Setter Property="Background">
            <Setter.Value>
              <ImageBrush ImageSource="..."/>
            </Setter.Value>
          </Setter>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Grid Background="{TemplateBinding Background}">
                </Grid>
                <ControlTemplate.Triggers>
                  <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background">
                      <Setter.Value>
                        <ImageBrush ImageSource="..."/>
                      </Setter.Value>
                    </Setter>
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>

     

    或者在ControlTemplate里面的触发器用 TargetName 来指定更改某个元素:

        <ControlTemplate x:Key="ComboBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">
          <Grid x:Name="grid">
            <Grid.Background>
              <ImageBrush ImageSource="..."/>
            </Grid.Background>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsMouseOver" Value="True">
              <Setter TargetName="grid" Property="Background">
                <Setter.Value>
                  <ImageBrush ImageSource="..."/>
                </Setter.Value>
              </Setter>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 Colinfc 2011年6月3日 9:15
    2011年6月3日 6:20
    版主
  • THX~~
    2011年6月3日 9:15