当按钮isEnabled为false时改变背景

时间:2015-05-24 09:28:28

标签: wpf xaml

我有一个带有按钮的Itemscontrol的Itemscontrol。这使得矩阵具有64个按钮。 每个按钮都绑定到ViewModel。按钮有一个有颜色的日食。

按钮可以有两种状态。

1)ViewModel在按钮中为日食提供颜色,然后按下按钮 ImageView img = (ImageView) findViewById(R.id.show_img); if (content.getImg().equalsIgnoreCase( "not")) { img.setVisibility(View.GONE); } else { img.setVisibility(View.VISIBLE); String image = content.getImg(); int imageResource = c.getResources().getIdentifier( "drawable/" + image, null, c.getPackageName()); img.setImageResource(imageResource); Log.i(DBAdapter.TAG, image); }
2)按钮IsEnabled=true,按钮不再可点击。

我会为数字2设置透明背景(当按钮未启用时)。我做了一些工作并得到了这个(参见Pastebin上的代码),但现在我的边界已经消失,一切都是透明的。

如何为每个始终可见的按钮获取边框,因此不限于1和2。

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

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题:

  1. 您正在直接在按钮上设置Background,BorderBrush和BorderThickness属性,而不是使用样式中的setter。以这种方式设置它们会覆盖样式所做的任何事情,因此您无法看到样式触发器的效果。这就是我所说的那句话:

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

    从该行中删除Background,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>
    
  3. 查看这些更改是否为您提供了所需的结果。

相关问题