Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在METRO应用程序中与DataTemplate一起使用GoToState_C#_Xaml_Visualstatemanager - Fatal编程技术网

C# 如何在METRO应用程序中与DataTemplate一起使用GoToState

C# 如何在METRO应用程序中与DataTemplate一起使用GoToState,c#,xaml,visualstatemanager,C#,Xaml,Visualstatemanager,在我的页面中。资源我有DataTamplate: <DataTemplate x:Key="gridviewQuestStyle"> <Button Content="{Binding QuestNumb}" Style="{StaticResource buttonQuestStyle}"> <VisualStateManager.VisualStateGroups> <Visu

在我的页面中。资源我有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>
我的应用程序的逻辑是:

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

请解释一下,如果页面或用户控件的.cs文件中(而不是MVVM视图模型中)出现VisualState切换,我在GoState方法的第一个参数中需要什么?

。将Name属性应用于GridView

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

Listview也有同样的问题。可以使用ItemContainerGenerator和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,请参见

然后可以编写ExtendedVisualStateManager.GoToElementState(…)

<GridView Grid.Row="0" x:Name="GridView_ButtonsQuest"
    ItemTemplate="{StaticResource gridviewQuestStyle}"
    ItemsSource="{Binding Questions}" 
    x:Name="myStateChanges" >
</GridView>
if(isAnswerRight)
{
  VisualStateManager.GoToState(this.myStateChanges, "Right", false);
}
else
{
  VisualStateManager.GoToState(this.myStateChanges, "Wrong", false);
}
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);
   ...