locked
How get VisualStateGroup for a Button control?

    Question

  • I have a test App that is 100% C++ code with no Xaml. It is based on the 'StrippedDownHello' example code provided in the Microsoft book 'Programming Windows 6th Edition' by Charles Petzold.

    I am trying to get the VisualStateGroups for a 'Button'. I have tried numerous ways, however the size of the VisualStateGroup vector is always zero. My latest code attempt is included below.

    In the code '_theView' is a 'Windows::UI::Xaml::Controls::Button^' and it is contained within a 'Windows::UI::Xaml::Controls::Canvas^'. The canvas is the root of the Visual Tree ie. it has been assigned to the Content property of the Apps Window ('Windows::UI::Xaml::Window::Current').

    Could someone please point me in the right direction to obtain the VisualStateGroups in this scenerio?

    Windows::Foundation::Collections::IVector<Windows::UI::Xaml::VisualStateGroup^>^ vsgs;
    Windows::UI::Xaml::DependencyObject^ obj;
    Windows::UI::Xaml::FrameworkElement^ prevObj;
    obj = _theView;
    prevObj = _theView;
    while (obj != nullptr)
    {
        Windows::UI::Xaml::FrameworkElement^ fe;
        fe = dynamic_cast<Windows::UI::Xaml::FrameworkElement^>(obj);
        if (fe != nullptr)
        {
            prevObj = fe;
            vsgs = Windows::UI::Xaml::VisualStateManager::GetVisualStateGroups(prevObj);
            int numStateGroups = vsgs->Size;
            if (numStateGroups > 0)
            {
                //Never Gets Here
                break;
            }
        }
        obj = prevObj->Parent;
    }
    

    Monday, April 15, 2013 10:38 AM

Answers

  • I re-asked the question as "How programmatically create and add a VisualStateGroup to a Xaml control?".

    This can be done in code by creating a Xaml 'Style' string programmatically as required and then loading it using XamlReader as follows:

    //xaml string contains full Xaml markup for <Style TargetType=\"Button\">
    //including VisualStateGroups, VisualStates and StoryBoards.
     
    Platform::Object^ xamlObj = Windows::UI::Xaml::Markup::XamlReader::Load(xaml);
    Windows::UI::Xaml::Style^ xamlStyle;
    xamlStyle = dynamic_cast<Windows::UI::Xaml::Style^>(xamlObj);
    if (xamlStyle != nullptr)
    {
       _theView->Style = xamlStyle;
    }
    

    • Marked as answer by Keith Slack Tuesday, April 16, 2013 6:46 PM
    Tuesday, April 16, 2013 6:46 PM

All replies

  • I'm now using 'Windows::UI::Xaml::Media::VisualTreeHelper' to recursively inspect my visual tree from its root.

    I am now getting some none-empty VisualStateGroup vectors returned.

    I will examine these and post back later.

    Monday, April 15, 2013 4:10 PM
  • Hi,

     

    Would you please provide us the XAML codes, so that we can reproduce your issue. Or you can upload a sample code project to skydrive

    http://skydrive.live.com/

     

    Best regards,

    Jesse



    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 16, 2013 7:10 AM
  • I re-asked the question as "How programmatically create and add a VisualStateGroup to a Xaml control?".

    This can be done in code by creating a Xaml 'Style' string programmatically as required and then loading it using XamlReader as follows:

    //xaml string contains full Xaml markup for <Style TargetType=\"Button\">
    //including VisualStateGroups, VisualStates and StoryBoards.
     
    Platform::Object^ xamlObj = Windows::UI::Xaml::Markup::XamlReader::Load(xaml);
    Windows::UI::Xaml::Style^ xamlStyle;
    xamlStyle = dynamic_cast<Windows::UI::Xaml::Style^>(xamlObj);
    if (xamlStyle != nullptr)
    {
       _theView->Style = xamlStyle;
    }
    

    • Marked as answer by Keith Slack Tuesday, April 16, 2013 6:46 PM
    Tuesday, April 16, 2013 6:46 PM