开发者

Image don't appear on button ControlTemplate WPF

开发者 https://www.devze.com 2023-03-09 22:43 出处:网络
I\'m having a problem with ControlTemplates. When I try to put a image in the button, the image don\'t appear on the application. Here the style:

I'm having a problem with ControlTemplates.

When I try to put a image in the button, the image don't appear on the application. Here the style:

    <Style x:Key="FocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Control}">
                    <Grid Margin="3 2">
                        <Rectangle Name="r1" StrokeThickness="1" Stroke="Black" StrokeDashArray="2 2"/>
                        <Border Name="border" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}"  CornerRadius="130" BorderThickness="1" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

  开发者_如何学运维  <Style x:Key="ShadowStyle">
        <Setter Property="Control.Foreground" Value="LightGray" />
    </Style>

    <Style x:Key="roundedButtonStyle" TargetType="Button">

        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="FontFamily" Value="Verdana"/>
        <Setter Property="FontSize" Value="11px"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />

        <Setter Property="Background" >
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,0.6" >
                    <GradientStop Color="#FFEEEEEE" Offset="0.2"/>
                    <GradientStop Color="#FFDDDDDD" Offset="0.85"/>
                    <GradientStop Color="#FFCCCCCC" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="border" BorderThickness="1" Padding="4,2" BorderBrush="#FFCCCCCC" CornerRadius="130,130,130,130" Background="{TemplateBinding Background}">
                        <Grid >
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="contentShadow" Style="{StaticResource ShadowStyle}">
                                <ContentPresenter.RenderTransform>
                                    <TranslateTransform X="1.0" Y="1.0" />
                                </ContentPresenter.RenderTransform>
                            </ContentPresenter>
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
                        </Grid>
                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="#FF4788c8" />
                            <Setter Property="Foreground" Value="#FF4788c8" />
                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,0.6" >
                                        <GradientStop Color="#FF87BEF5" Offset="0.2"/>
                                        <GradientStop Color="AliceBlue" Offset="0.85"/>
                                        <GradientStop Color="#FF87BEF5" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>

                            <Setter TargetName="content" Property="RenderTransform" >
                                <Setter.Value>
                                    <TranslateTransform Y="1.0" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>

                        <Trigger Property="IsDefaulted" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
                        </Trigger>

                        <Trigger Property="IsFocused" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="border" Property="Opacity" Value="0.7" />
                            <Setter Property="Foreground" Value="Gray" />
                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>        

And I try to put the image in the button like this:

            <Button Name="btnZoom" Style="{StaticResource roundedButtonStyle}" Width="25" Height="25" Margin="5" Click="btnZoom_Click">
                <StackPanel>
                    <Image Source="/Resources/lupa.png"/>
                </StackPanel>
            </Button>  

In the visual editor of Visual Studio 2010 the image is showed, but when I run the application the image is not there.

And please, forgive my horrible english.

Thanx.


Make sure the Build Action of the image is set to Resource (not Content or Embedded Resource)


Cannot reproduce it.

Try it in a test project, your context might be at fault here, try to narrow down the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号