Hi,

I'm reasonably new to C# and even newer to WPF projects in C#, i'm having a lot of trouble getting to grips with how to use the template property and its doing m head in. Firstly an explanation of what I am trying to achieve:
I am working on a very large project and thought it would be tidy to have any templated types (for visual styles) stored in an external dll (for reusabilities sake), the first thing I have come to that I would like for a visual style is a transparent button (setting borderbrush and background to transparent still leaves a strange chrome effect behind the button (tedious)).
I have tried following various guides on the internet but am now getting a vague error (Cannot unset a Setter value) of which there is documentation.
I really do need to get to grips (especially with the visual aspect) as quick as possible.
The solution I would prefer but dont think is possible would be to just say display the button as a label with transparent background and border (while still keeping the button events) but after looking at a post on another website it made it seem like this was not possible.

Any help would be greatly appreciated,

Thanks,

Chris

Recommended Answers

All 2 Replies

Show us your code please.

<Window.Resources> 
        <ControlTemplate x:Key="ts"  TargetType="{x:Type Button}" >
            <Border Name="tt" CornerRadius="4"  Focusable="True"  Background="Transparent" HorizontalAlignment="Center" Padding="3" Margin="5" BorderBrush="Black" BorderThickness="2">
                <ContentPresenter />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Control.IsMouseOver" Value="True">
                    <Setter TargetName="tt" Property="Background" Value="Red"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        
    </Window.Resources>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel.Background>
            <ImageBrush  ImageSource="Images/1.jpg" TileMode="Tile" Viewbox="0,0,.3,.4"/>
        </StackPanel.Background>
        <Button Template="{StaticResource ts}" 
                Content="Dust"
                Click="Button_Click"
                >
        </Button>
        <Button Template="{StaticResource ts}">Test</Button>
    </StackPanel>

Hi, thanks for the response, the way i've been try to use it is as follows:

one dll that contains an xaml file with the following
dll is called DisplayTemplates and xaml file is called transparentbutton.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="TransparentButton" TargetType="Button">
        <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <StackPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" x:Name="panel">
                            <ContentPresenter Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" />
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" Value="Transparent" TargetName="panel" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="Transparent" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

    </Style>
</ResourceDictionary>

(although as you can see this is where i'm struggling, as i don't really understand the template concept yet)

and then using in another file as follows

<Window x:Class="MessageDisplay.message_display_window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="Error" Height="262" Width="602" FontFamily="Verdana" IsTabStop="True" TabIndex="1" ResizeMode="NoResize" Loaded="Window_Loaded">
    <Window.Resources>
        <ResourceDictionary Source="pack://application:,,,/DisplayTemplates;component/transparentbutton.xaml"/>
    </Window.Resources>
    <Grid Visibility="Visible">
        <Label Margin="12,12,10,0" Name="lbl_ErrorDescription" BorderBrush="DarkGray" BorderThickness="2" Height="148" VerticalAlignment="Top"></Label>
        <ListView Height="88" Width="558"  Margin="12,182,10,0" Name="lsv_ErrorDetails" BorderBrush="DarkGray" BorderThickness="2" VerticalAlignment="Top" Visibility="Hidden" ItemsSource="{Binding ErrorCollection}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="30" Header="Name"
                        DisplayMemberBinding="{Binding ErrorName}"  />
                    <GridViewColumn Width="140" Header="Details"  
                        DisplayMemberBinding="{Binding ErrorDetail}" />
                 </GridView>
            </ListView.View>
        </ListView>
        <Label Height="22" Margin="12,160,192,44" Name="lbl_TimeOfError" VerticalAlignment="Top">Time Of Error:</Label>
        
        <Button Height="22" HorizontalAlignment="Right" HorizontalContentAlignment="Right"  Margin="0,160,10,0" Name="btn_ShowDetails" VerticalAlignment="Top" Width="130" Foreground="Blue" Cursor="Hand" Focusable="True" IsTabStop="True" TabIndex="1" Click="btn_ShowDetails_Click" Style="{StaticResource TransparentButton}">Show details...</Button>
        <Button Height="34" Margin="230,0,228,5" Name="btn_Ok" VerticalAlignment="Bottom" Click="btn_Ok_Click">Ok</Button>
        
        
    </Grid>
</Window>

Thanks,

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.