0

我有奇怪的问题。我有一个带有按钮的表单。我将按钮的背景颜色设置为Blue,如下图所示。

在此处输入图像描述

当我将鼠标移到它上面时,它会变成浅蓝色。

在此处输入图像描述

现在我按下鼠标左键并且不离开按钮,将鼠标拖动到按钮边界之外,然后单击鼠标。然后按钮开始闪烁并在这两个图像之间更改颜色,并且在我单击任何其他控件之前永远不会停止。另外,我有一个附加到这个控件的命令。如果我单击此控件,则命令将执行,并且该按钮再次开始在这两个图像之间闪烁,并且在我单击任何其他控件(如文本框、复选框或其他内容)之前永远不会停止。谁能帮我理解这一点。

下面是按钮的`XAML。

    <Button x:Name="BtnConvert" Content="Start Conversion Process" 
            Margin="0,436,433,0" 
            Height="108" Command="{Binding StartConvertProcess}" 
            VerticalAlignment="Top" HorizontalAlignment="Right" 
            Width="236" FontSize="14" Background="#FF5998B6"/>

编辑

视图模型代码

    private void StartConvertProcessMethod()
    {
        string errorMessage;
        bool result = _model.ConvertFiles(SourcePath, TargetPath, DataSource, out errorMessage);

        string caption;
        MessageBoxIcon messageBoxIcon;

        if (result)
        {
            caption = "Completed";
            messageBoxIcon = MessageBoxIcon.None;
        }
        else
        {
            caption = "Completed with Errors";
            messageBoxIcon = MessageBoxIcon.Error;
        }

        MessageBox.Show(errorMessage, caption, MessageBoxButtons.OK, messageBoxIcon);
    }
4

1 回答 1

0

我通过为我的按钮定义 ControlTemplate 解决了这个问题。像这样的东西:

<ControlTemplate TargetType="{x:Type Button}" x:Key="PlainButtonTemplate">
        <Border x:Name="bdr_main" BorderThickness="1" BorderBrush="LightGray" CornerRadius="6"  Background="Transparent">
        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" ContentSource="Content" />
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="bdr_main" Property="Background" Value="Transparent"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter TargetName="bdr_main" Property="Background" Value="Gainsboro"/>
            </Trigger>
        </ControlTemplate.Triggers>

然后将模板应用到您的按钮。

<Style x:Key="PlainButton" TargetType="{x:Type Button}">
        ...
        <Setter Property="Template" Value="{DynamicResource PlainButtonTemplate}"></Setter>
        ...
</Style>

例如,将模板和样式放在单独的 ResourceDictionary 中,然后在使用按钮的页面中引用字典:

<Window.Resources>
 <ResourceDictionary>
  <ResourceDictionary x:Key="Dict" Source="ResourceDictionaries\ButtonsDictionary.xaml" />                
 </ResourceDictionary>        
</Window.Resources>
于 2013-06-06T11:36:39.383 回答