无法设置按钮内容 - WPF

时间:2017-07-30 15:36:41

标签: c# wpf button

<UserControl x:Class="rd_terminal.terminal_pages.button_controls.red_button.exiPadButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="79" d:DesignWidth="256"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Button  Name="button" Width="256" Height="79" Click="button_Click_1" FontSize="20" FontWeight="Bold">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border Name="buttonBorder">
                <Border.Effect>
                    <DropShadowEffect Opacity="0.0" />
                </Border.Effect>
                <Border.Child>
                    <Image Name="img" Source="{Binding NormalImage}"/>
                </Border.Child>
            </Border>

            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="buttonBorder" Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect Opacity="0.0" />
                        </Setter.Value>
                    </Setter>

                </Trigger>
                <Trigger Property="IsMouseCaptured" Value="true">
                    <Setter TargetName="buttonBorder" Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect Opacity="0" Direction="135"  
                         ShadowDepth="0" BlurRadius="0" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger Property="IsPressed" Value="true">
                    <Setter TargetName="img" Property="Source" Value="{Binding DisabledImage}"/>
                    <Setter TargetName="buttonBorder" Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect Opacity="0.0"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>

</Button>

我有这个按钮用户控件。它工作正常,直到我添加内容,然后它不再工作。它甚至无法点击,当内容词进入时图像不会改变。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

WPF按钮将需要内容演示者来呈现内容类型。您必须在控件模板中添加内容展示器。  请尝试以下修改后的代码:

<UserControl>
<UserControl.Resources>     
    <Style x:Key="ImageEnabled" TargetType="Image">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.25"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Button  Name="button" Width="256" Height="79" Click="button_Click_1" FontSize="20" FontWeight="Bold">
      <Image Source="{StaticResource NormalImage}" Style="{StaticResource ImageEnabled}"/>
    <Button.Template> 

<ControlTemplate TargetType="{x:Type Button}">
                        <Border Name="buttonBorder">
                            <Border.Effect>
                                <DropShadowEffect Opacity="0.0" />
                            </Border.Effect>
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="buttonBorder" Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect Opacity="0.0" />
                                    </Setter.Value>
                                </Setter>

                            </Trigger>
                            <Trigger Property="IsMouseCaptured" Value="true">
                                <Setter TargetName="buttonBorder" Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect Opacity="0" Direction="135"  
                         ShadowDepth="0" BlurRadius="0" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter TargetName="buttonBorder" Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect Opacity="0.0"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
 </Button.template>
</Button>
</UserControl>