Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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
Silverlight 我可以在ItemTemplate中更改DataTemplate的VisualState吗?_Silverlight_Windows Phone 7_Xaml_Datatemplate_Visualstatemanager - Fatal编程技术网

Silverlight 我可以在ItemTemplate中更改DataTemplate的VisualState吗?

Silverlight 我可以在ItemTemplate中更改DataTemplate的VisualState吗?,silverlight,windows-phone-7,xaml,datatemplate,visualstatemanager,Silverlight,Windows Phone 7,Xaml,Datatemplate,Visualstatemanager,我在DataTemplate中有一些控件,我想控制它的按下状态行为。我在数据模板中加入了VisualStateManager,但它似乎不起作用。我想有可能理解我下面要做的事情。是否可以在DataTemplate标记内进行内联操作 <ItemsControl ItemsSource="{Binding Items}"> .... <ItemsControl.ItemTemplate> <DataTemplate>

我在DataTemplate中有一些控件,我想控制它的按下状态行为。我在数据模板中加入了VisualStateManager,但它似乎不起作用。我想有可能理解我下面要做的事情。是否可以在DataTemplate标记内进行内联操作

<ItemsControl ItemsSource="{Binding Items}">
    ....
    <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Grid ...>
             <VisualStateManager.VisualStateGroups>
                 <VisualStateGroup x:Name="CommonStates">
                     ...
                     <VisualState x:Name="Pressed">
                         <Storyboard>
                             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderThickness" Storyboard.TargetName="GridItemBorder">
                                 <DiscreteObjectKeyFrame KeyTime="0" Value="3"/>
                              </ObjectAnimationUsingKeyFrames>
                         </Storyboard>
                     </VisualState>
                 </VisualStateGroup>
             </VisualStateManager.VisualStateGroups>
             <Border x:Name="Border" ...>
                 ...
             </Border>
          </Grid>
      </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

....
...
...

简单的回答是,目标控件类型没有“按下”的可视状态——因此,尽管您可以在可视状态管理器中引用任何状态,但这并不重要,因为控件的代码永远不会将其置于该状态

通过查看控件的定义(它们是使用
TemplateVisualState
属性声明的),或者通过查看

这里的方法可能是使用
按钮
(或者重写您编写的
[ButtonBase][2]
),因为它内置了“按下”的视觉状态。您只需为它编写一个控制模板,以提供所需的布局/样式


编辑以下是一个示例:

控件模板(参考资料部分)。这是
按钮的控件模板,但实际上不是按钮。我只是用它来利用“按下”的视觉状态功能

    <ControlTemplate x:Key="MyButtonTemplate" TargetType="Button">
        <Grid>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Pressed">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="GridItemBorder">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="3"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Border x:Name="GridItemBorder" BorderBrush="Orange" BorderThickness="1" Background="White">
                <ContentPresenter Content="{TemplateBinding Content}" />
            </Border>
        </Grid>
    </ControlTemplate>

项目控制

将项目模板定义为使用上述ControlTemplate的“按钮”

    <ItemsControl ItemsSource="{Binding SelectedItems}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Template="{StaticResource MyButtonTemplate}" Content="{Binding}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>