-1

I have a XAML button control that looks like so:

<Button x:Name="my-button" Grid.Row="3" Content="See More Foo" Click="my-button_Click" Style="{StaticResource BlueButtonStyle}" Width="75" Height="23" HorizontalAlignment="Right" Margin="10,20,90,0" />

I'd like to change the style of the button in a C# method along the lines of:

public void ChangeButtonColor()
{
    //Change the style attribute of my-button to RedButtonStyle
}

How would I go about accessing the XAML control in the C# code behind?

This is in a Silverlight 4.0 application.

Zach B
  • 534
  • 7
  • 25
  • 1
    You don't do it that way. Nor would you want to do it in code behind. You may want to go [read up](http://msdn.microsoft.com/en-us/library/cc278069(vs.95).aspx) a bit on how [control style templating works](http://msdn.microsoft.com/en-us/library/cc189093(v=vs.95).aspx) amigo. Oh and I wasn't your down-voter, but don't be dissuaded if you get more. Just need to get on the right path. – Chris W. Sep 10 '14 at 18:15
  • Thanks for the recommend, starting my read now. Building out ControlTemplates seem like a heavy handed way and lot of unnecessary code to address a style change when the style I'm looking for is already defined as an application resource. My thoughts were that if X happens, then do Y - with Y being to change the style definition of the control to the other defined style. Do events not work that way in Silverlight? – Zach B Sep 10 '14 at 18:24
  • Was just talking with a colleague... maybe the actual question I am trying to ask is how do you switch out templates of a XAML control using C#? – Zach B Sep 10 '14 at 18:32
  • 1
    Eh, you've got options, could just build your change into the style template and flip it with an additional VisualState in the VisualStateManager and a GoToState action, or maybe even a quick ChangePropertyAction on something like a DataTrigger to flip its ControlTemplate, or there's other possible solutions as well, bit strapped on time today but I'll see if I can't dig around for a tutorial or something here in a bit if I cant find time to just answer with an example later. – Chris W. Sep 10 '14 at 18:57
  • VisualState properties are defined in the style definitions. I'm trying to get to the 'Disabled' state. By default the 'Normal' state is applied. Looked into using `VisualStateManager.GoToState(my-button, "Disabled", false);` but can't get the state to actually change using GoToState() that way. I'll do some more exploring today and see if I can get it to work. Thanks for the push in the right direction. – Zach B Sep 10 '14 at 19:11
  • 1
    If all you want is the Disabled state, just throw that bool at the IsEnabled property of your button and it will handle it from there. Depending on the condition though you might have to notify of the property change, but should be easy peasy. – Chris W. Sep 10 '14 at 19:13
  • That definitely works, but I'm back to my original issue, which is that my button has XYZ properties defined (in this case IsEnabled='false') and I have an action that occurs in my C# code behind that need to change the definition of XYZ property. I'm thinking that I need to learn more about `VisualState` management. – Zach B Sep 10 '14 at 19:19
  • 1
    Nah, sounds like you just need to bind it, like `` at least if I'm understanding ya correctly anyway. – Chris W. Sep 10 '14 at 19:21

1 Answers1

1

Not a complete solution, but I found a workaround that helped me, so I thought I would share. The objective of changing the style of the button was to effectively disable it. So, instead of changing the style, I set the IsEnabled attribute of the XAML control to "false".

Using C#, I was able to change the IsEnabled attribute definition by accessing the control directly via:

public void EnableButton()
{
    my-button.IsEnabled = true;
}

Though it looks like styles could have been affected through some configuration of the VisualStateManager, I did not end up having to go that far.

Thanks @Chris W. for the discussion thread and helping me get here!

Zach B
  • 534
  • 7
  • 25
  • Getting started with the VisualStateManger is difficult without using a tool like Blend. If you have Blend installed, check this out, it may help in the fugure... http://stackoverflow.com/questions/3978183/blinking-button-on-wpf-application/5208927#5208927 – Onosa Sep 10 '14 at 20:12