How to use GoToState with a DataTemplate in a METRO application

In my page. Resources I have a DataTamplate:

<DataTemplate x:Key="gridviewQuestStyle">
        <Button Content="{Binding QuestNumb}" Style="{StaticResource buttonQuestStyle}"> 
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="questionStates">                        
                    <VisualState x:Name="Right">
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetName="BackgroundBrush" Storyboard.TargetProperty="Color" To="LightGreen" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Wrong">
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetName="BackgroundBrush" Storyboard.TargetProperty="Color" To="Red" />
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

            <Button.Background>
                <SolidColorBrush x:Name="BackgroundBrush" Color="Black"/>
            </Button.Background>
        </Button>
    </DataTemplate>

Then I create a GridView:

<GridView Grid.Row="0" x:Name="GridView_ButtonsQuest"
    ItemTemplate="{StaticResource gridviewQuestStyle}"
    ItemsSource="{Binding Questions}" >
</GridView>

Questions is a list:

public class Question
{
    public string QuestNumb { get; set; }
    public string QuestText { get; set; }
}

The logic of my application:

    if(isAnswerRight)
{
  VisualStateManager.GoToState(???, "Right", false);
}
else
{
  VisualStateManager.GoToState(???, "Wrong", false);
}

Please explain what I need in the first parameter in the GoToState method?

+3
source share
2 answers

If the VisualState switch appears in the .cs file for the page or UserControl (not in the MVVM View Model). Apply the Name property to the GridView

<GridView Grid.Row="0" x:Name="GridView_ButtonsQuest"
    ItemTemplate="{StaticResource gridviewQuestStyle}"
    ItemsSource="{Binding Questions}" 
    x:Name="myStateChanges" >
</GridView>

then and paste it into the GoToState () method.

if(isAnswerRight)
{
  VisualStateManager.GoToState(this.myStateChanges, "Right", false);
}
else
{
  VisualStateManager.GoToState(this.myStateChanges, "Wrong", false);
}
+1
source

Had the same list problem. The control inside the template can be accessed using ItemContainerGenerator and VisualTreeHelper

foreach (var item in GridView_ButtonsQuest.Items)
{
    var gridItem = (GridViewItem)MyList.ItemContainerGenerator.ContainerFromItem(item);
    var wrap1 =VisualTreeHelper.GetChild(gridItem , 0);
    var wrap2 = VisualTreeHelper.GetChild(wrap1 , 0);
   ...

xmalspy , . -, .

, , , GoToState . - VisualStateManager, , . http://social.msdn.microsoft.com/Forums/en-GB/winappswithcsharp/thread/24dc19ff-15ed-4170-b3c3-d313728b642b

ExtendedVisualStateManager.GoToElementState(...)

+1

All Articles