禁用按钮背景消失

时间:2014-05-08 14:36:48

标签: c# wpf button background windows-phone

我对此感到非常惊讶,但似乎禁用Windows手机上的按钮会导致其背景消失。

我已经阅读了一些XAML解决方案,但是,我的按钮是以编程方式创建的(我不会改变它),我不知道如何处理它。有谁知道如何解决这个问题?

table[i, j] = new Button();
table[i, j].Content = "";
table[i, j].BorderThickness = new Thickness(0, 0, 0, 0);
table[i, j].Height = 70;
table[i, j].Width = 70;
table[i, j].Background = brush1;
Thickness m = table[i, j].Margin;
m.Left = -18;
m.Top = -20;
table[i, j].Foreground = new SolidColorBrush(Colors.White);
table[i, j].Margin = m;
table[i, j].Tap += CoolMethod;
//The button is Added to a Grid
//Visible
//.......

//CoolMethod(){
table[i, j].IsEnabled = false;
//No longer visible
table[i, j].Background = brush2;
//Still not visible

背景资源现在是像这样的ImageBrush:

ImageBrush buttonUnavailableImage = new ImageBrush();
BitmapImage imageDisabled = new BitmapImage(new Uri("Images/caseinactiv.png", UriKind.Relative));
buttonUnavailableImage.ImageSource = imageDisabled;

1 个答案:

答案 0 :(得分:2)

您正在观察的行为是由按钮的默认控件模板引起的。

要查看WP7.1的默认样式,请打开该文件 “C:\ Program Files(x86)\ Microsoft SDKs \ Windows Phone \ v7.1 \ Design \ System.Windows.xaml”文件并搜索<Style x:Key="PhoneButtonBase" TargetType="ButtonBase">

<Style x:Key="PhoneButtonBase" TargetType="ButtonBase">
    <!-- skipped.. -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ButtonBase">
                <Grid Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed"><!-- skipped.. --></VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}" >
                        <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

你看到<VisualState x:Name="Disabled">元素下发生了什么?边框的背景由Transparent画笔替换。

要修改该样式,请将该样式复制并粘贴到app.xaml中,然后根据需要编辑按钮控件模板的Disabled可视状态。

指定无x:Key属性以将该样式应用于应用程序中的所有按钮。或者,您可以指定一些键,并在按钮创建代码中添加以下行以手动应用该样式: theButton.Style = (Style)Application.Current.Resources[ "styleKey" ];