Good day, Hashcode.

I have a User Control , with a picture in the background. Described as follows:

  <Button some_parameters_here> <Button.Background> <ImageBrush ImageSource="wayToPicture.png" /> </Button.Background> </Button> 

Now, when I change the value of the IsEnabled property of the button, the background disappears when the value is false , i.e. as if visibility changed. How can I make a color change instead, as in a normal control? (i.e., when IsEnabled = false , the picture will become black and white)

As I understand it, you have to declare your class and inherit it from the control / button (what exactly?) And overload the IsEnabled method. Honestly, I am not strong in inheritance, please help.

 public class PaintedButton: Button // Control? { PaintedButton() :base() { } public override ??? { } } 

PS So, I discovered the difference between WPF and Silverlight.

    2 answers 2

    Hidden by the fact that you have a theme PresentationFramework.Aero or others. Not quite standard. In the case of Enable brush changes and your picture disappears. You can make the content adjustable, but I do not advise. I suggest, as an option, a very simple, in my opinion, button template. It switches one image to another depending on the properties. By analogy, you can learn and other things in WPF .

     <Button IsEnabled="True"> <Rectangle Width="100" Height="100" Fill="Blue" /> - здесь ваш контент или текст <Button.Template> <ControlTemplate TargetType="Button"> <Grid> <Image Stretch="Fill" x:Name="EnabledT" Source="/WpfApplication1;component/Images/EnabledPic.jpg" /> <Image Stretch="Fill" x:Name="DisabledT" Visibility="Hidden" Source="/WpfApplication1;component/Images/DisablePic.jpg" /> <ContentPresenter /> - Контент </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="True"> <Setter TargetName="EnabledT" Property="Visibility" Value="Visible" /> <Setter TargetName="DisabledT" Property="Visibility" Value="Hidden" /> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="EnabledT" Property="Visibility" Value="Hidden" /> <Setter TargetName="DisabledT" Property="Visibility" Value="Visible" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Button.Template> </Button> 
    • For WPF of course it works. :) Sorry, wrong with the label. Plus put. For Silverlight, you probably need to poke over the VisualStateManager. - Olter

    It's time to close the question, so the solution is :

    In app.xaml, we create our own style for the button, find VisualState = pressed and specify our own version there. In the simplest case, we stupidly erase everything that is written there, then, when pressed, there will be no reaction to it. (i.e. without a fun white square)

      <Style TargetType="Button"> ... <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="MouseOver"/> <VisualState x:Name="Pressed"/> <-- Если хотим что-то менять, то дальше пишем <StoryBoard> -->