Muhammad Danish
Muhammad Danish

Reputation: 109

Getting exception while applying style in on button in wpf

I have 2 button on window. I am applying the style. but got this exception.

'Set property 'System.Windows.FrameworkElement.Style' threw an exception.' Line number '38' and line position '7'.

My Code.

<Window.Resources>
        <Image x:Key="btnconnect" Source="images/img-1.png" /> 
        <Image x:Key="btnshow" Source="images/img-2.png" />
        <Image x:Key="btnclick" Source="images/img-2clicked.png"/>
        <Style x:Key="buttonconnect" TargetType="Button">
            <Setter Property="Content" Value="{StaticResource btnconnect}">
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid x:Name="grid">
                            <Border x:Name="border" CornerRadius="8" BorderThickness="2">
                                <ContentPresenter HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            TextElement.FontWeight="Bold"></ContentPresenter>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Value="{StaticResource btnclick}" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" TargetName="grid" Value="0.25"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="buttonshow" TargetType="{x:Type Button}">
            <Setter Property="Content" Value="{StaticResource btnshow}" />
        </Style>
    </Window.Resources>
    <Grid>
        <Button Margin="50 100 50 100" Style="{StaticResource buttonconnect}" Height="50" Width="120"/>
        <Button Margin="50 110 50 10" Style="{StaticResource buttonshow}" Height="50" Width="120"/>
    </Grid>

Upvotes: 0

Views: 807

Answers (1)

franssu
franssu

Reputation: 2430

<Setter Value="{StaticResource btnclick}" />

I think your forgot to specify the property on which the setter applies.

Although I'm not sure it would cause a runtime error.

Basically what you should do (ideally before posting) is try to remove as much code as you can while still being able to reproduce your issue, only then post your question with the simplified code if you still have no clue of where the error is coming from.

Upvotes: 1

Related Questions