2015-05-24 110 views
0

我有一个Itemscontrol与Itemscontrol按钮。这使得有64个按钮的矩阵。 每个按钮都绑定到一个ViewModel。按钮有一个颜色的日食。当按钮isEnabled是假更改背景

一个按钮可以有两种状态。

1)ViewModel为按钮上的eclipse提供颜色,然后按钮IsEnabled=true
2)按钮IsEnabled=false并且该按钮不再可点击。

我想为编号2的透明背景(当按钮没有启用时)。我开展了一些工作,并获得了这一点(请参阅Pastebin上的代码),但现在我的边界已经消失,一切都变得透明。

我怎样才能得到一个边界为每个按钮是可见所有的时间,所以它不局限于号码1和2

这里是我的代码。我把它放在Pastebin上以节省空间。 引擎收录:http://pastebin.com/4GHCW0y8 感谢

回答

1

有几个问题与您的代码:

  1. 你设置背景,直接在您的按钮,而不是在风格上使用制定者BorderBrush和性能了borderThickness。以这种方式设置它们会覆盖样式所做的任何操作,因此您无法看到样式触发器的效果。这是我指的是行:

    <Button Style="{StaticResource MyButton2}" Command="{Binding PlaceStoneCommand}" Background="Transparent" BorderBrush="Black" BorderThickness="0.75" Width="30" Height="30"> 
    

    删除背景,从该行BorderBrush和了borderThickness属性,并在样式做setter方法代替。此外,请注意,您将默认设置为透明,因此即使进行此更改,您也只是在IsEnabled更改时在“透明”和“透明”之间切换。

  2. 您的样式将覆盖按钮的控件模板,并且不会在模板中使用BorderBrush或BorderThickness属性,因此在按钮上设置这些属性不会产生任何效果。您可能想要为模板中定义的边框上的这些属性设置模板绑定。所以,你最终会得到一个样式看起来像这样:

    <Style TargetType="{x:Type Button}" x:Key="MyButton2"> 
        <!-- Put default values here, or remove these if you want to use the existing button defaults --> 
        <Setter Property="Background" Value="Transparent"/> 
        <Setter Property="BorderBrush" Value="Black" /> 
        <Setter Property="BorderThickness" Value="1" /> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type Button}"> 
           <Border Background="{TemplateBinding Background}" 
             BorderBrush="{TemplateBinding BorderBrush}" 
             BorderThickness="{TemplateBinding BorderThickness}"> 
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
           </Border> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
        <Style.Triggers> 
         <Trigger Property="IsEnabled" Value="False"> 
          <Setter Property="Background" Value="Transparent"/> 
          <Setter Property="BorderBrush" Value="Black" /> 
          <Setter Property="BorderThickness" Value="1" /> 
         </Trigger> 
        </Style.Triggers> 
    </Style> 
    

看看这些变化给你想要的结果。

+0

感谢您的帮助和解决方案。它工作超级好。谷歌搜索后,我来到另一个解决方案,它更短,并不适用于所有的按钮。 链接至代码:http://pastebin.com/8qQH59pN – user3004449