在一个wpf项目中,我有这个XAML代码
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
x:Class="WpfApplication1.MainWindow"
xmlns:vsm="clr-namespace:System.Windows;assembly=WPFToolkit"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="VisualStateGroup">
<vsm:VisualState x:Name="Loading">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="control" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="button1" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<VisualState x:Name="Normal">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="control" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<ControlTemplate x:Key="loadingAnimation">
<Image x:Name="content" Opacity="1">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing> …Run Code Online (Sandbox Code Playgroud) 我尝试VisualStateManager.GetVisualStateGroups在OnAttached自定义行为的覆盖中使用,以及在添加到AssociatedObject.Loaded该行为中的事件的事件处理程序中使用。两次我都得到一个空列表。
是否有另一种方法来获取为控件定义的视觉状态组,或者我应该附加到的另一个事件处理程序?
因为它被问到了,是的,控件有VisualStateGroups和VisualStates。
c# silverlight visualstatemanager attachedbehaviors silverlight-4.0
我有一个ScrollViewer:
<ScrollViewer Width="160"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Hidden"
Height="324" Canvas.Top="0"
BorderThickness="0" Padding="0">
<ListBox x:Name="Snapshots" SelectionChanged="Snapshots_SelectionChanged"
Padding="0" Background="#FFF0F0F0"
BorderBrush="{x:Null}" VerticalAlignment="Top"
SelectionMode="Single">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding imageSource}"
Margin="5" Stretch="UniformToFill"
Width="120" Opacity="0.2"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"
VerticalAlignment="Top" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListBox>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
我该如何实现这些功能:
这是我的实际按钮样式:
<Style x:Key="CategoryButtonStyle" TargetType="{x:Type Button}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="grid">
<Path x:Name="TabPath" StrokeThickness="2"
Margin="{Binding ElementName=buttonContent, Converter={x:Static c:ContentToMarginConverter.Value}}"
Stroke="{StaticResource BorderBrush1}"
Fill="{StaticResource TabItemPathBrush}">
<Path.Data>
<PathGeometry>
<PathFigure IsClosed="False" StartPoint="1,0"
Segments="{Binding ElementName=buttonContent, Converter={x:Static c:ContentToPathConverter.Value}}">
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.LayoutTransform>
<!-- For some reason -->
<ScaleTransform ScaleY="-1"/>
</Path.LayoutTransform>
</Path>
<Rectangle x:Name="TabItemTopBorder" Height="2" Visibility="Visible"
VerticalAlignment="Bottom" Fill="{StaticResource BorderBrush1}"
Margin="{Binding ElementName=TabPath, Path=Margin}" />
<ContentPresenter x:Name="buttonContent" Margin="10,2,10,2" VerticalAlignment="Center"
TextElement.Foreground="{StaticResource ForegroundBrush}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Fill" TargetName="TabPath"> …Run Code Online (Sandbox Code Playgroud) 我在 Button 控件的 VisualStateGroup 中有一个 VisualTransition。Storyboard 属性绑定到 StaticResource Storyboard:
<VisualTransition From="MyLock" To="Unlocked" GeneratedDuration="0:0:2.0" Storyboard="{StaticResource Storyboard_Locked_ToUnlocked}"/>
Run Code Online (Sandbox Code Playgroud)
当我使用 VisualStateManager 进入“解锁”状态时,出现以下异常:“System.InvalidOperationException:类型为‘System.Windows.Media.Animation.Storyboard’的指定值必须将 IsFrozen 设置为 false 才能修改。” 故事板中的任何内容都没有修改故事板本身,所以我不确定这意味着什么。
两件奇怪的事。第一,这种情况直到从 VS 2010 迁移到 VS 2012 并安装 .Net 4.5 框架后才开始发生。其次,如果我复制所有代码并将其移动到标签内的 Storyboard 属性本身,则不会出现错误。所以它似乎不是故事板本身的任何东西,只是为此使用 StaticResource 的问题。
在研究时,我唯一能找到的关于订阅故事板的 Completed 事件的错误处理,我不会在任何地方做,除非 VisualStateManager 以某种方式这样做。
在此先感谢您的帮助。
编辑:我还应该补充一点,我想在两个不同的转换中使用它,这就是为什么我更喜欢它是一个静态资源,这样我就不必复制/粘贴 xaml。我注释掉了其中一个转换,但仍然出现错误,所以这也不是我正在共享它的事实。
我正在制作一个Win Store应用程序,并面临处理方向的问题.
有一个网格,我想在方向改变时修改它的设计.两侧的堆叠面板(在横向视图中)应在纵向视图中转到顶部和底部.
目前,我已经使用所有控件创建了两个网格,并根据方向调整它们,并使用VisualStateManager切换可见性属性.
但是我想用一个网格实现这个目标,任何建议对我都有很大的帮助.
下图是应用程序的概念设计:

当屏幕的方向改变时,我称之为:
string CurrentViewState = ApplicationView.GetForCurrentView().Orientation.ToString();
// Trigger the Visual State Manager
bool success = VisualStateManager.GoToState(this, CurrentViewState, true);
Run Code Online (Sandbox Code Playgroud)
在我的XAML中有一个简单的GRID(在页面层次结构的深处)
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Rectangle x:Name="rect_1" Grid.Column="0" Fill="Blue"></Rectangle>
<Rectangle x:Name="rect_2" Grid.Column="1" Fill="Red"></Rectangle>
<Rectangle x:Name="rect_3" Grid.Column="2" Fill="Green"></Rectangle>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Landscape">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="rect_1"
Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="Brown"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Portrait">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="rect_1"
Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="Orange"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
Run Code Online (Sandbox Code Playgroud)
所以它是3个简单的矩形,但它们的颜色永远不会改变.所有这一切都是采取直接从这里的教程:http://msdn.microsoft.com/en-us/library/windows/apps/dn495655.aspx唯一的区别是,我的网是不是页面的顶部元素.问题可能是第一个参数
VisualStateManager.GoToState(this, CurrentViewState, true);
Run Code Online (Sandbox Code Playgroud)
这是".但我不知道应该是什么,将其设置为网格或不允许其中一个矩形.
IList<VisualStateGroup> visualStateGroups = VisualStateManager.GetVisualStateGroups(this);
int …Run Code Online (Sandbox Code Playgroud) 嗨,我创建了一个ImagenButton如下:
<ImageButton Source="articulos.png" Clicked="ImageButton_Clicked"/>
Run Code Online (Sandbox Code Playgroud)
这是动画的代码:
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale" Value="1"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale" Value="0.8"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
Run Code Online (Sandbox Code Playgroud)
但我想创建许多具有相同样式的按钮,我不想重复代码。
这是完整的代码:
<ImageButton Source="articulos.png" Clicked="ImageButton_Clicked">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale" Value="1"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale" Value="0.8"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</ImageButton>
Run Code Online (Sandbox Code Playgroud)
请帮忙。
我有一个网格布局,其中包含7个元素.如果windowScreenWidth小于X,我想将最后4个元素从第一行移动到第二行.我已经添加了一个组和状态.如果我使用<Setter Target="el4.Grid.Row" Value="1"/>或<Setter Target="el4" Property="Grid.Row" Value="1"/>xaml抛出异常.
有没有办法让我想要的东西?
我创建了一个GridView通过数据绑定获取数据。我接下来要做的是根据屏幕大小使GridView项目的宽度动态变化(就像他们在Windows 10新闻,体育应用程序等中所做的那样)。到目前为止,我已经成功使用最小窗口宽度0做到了视觉状态管理器,方法是将水平对齐方式设置为拉伸,但是对于其他更大的窗口尺寸,我将无法继续进行。
任何用于解决此问题的帮助将不胜感激。
xaml ×4
c# ×3
wpf ×3
silverlight ×2
button ×1
grid-layout ×1
gridview ×1
listbox ×1
storyboard ×1
styles ×1
uwp ×1
windows-10 ×1
winrt-xaml ×1
wpftoolkit ×1
xamarin ×1