我正在使用以下代码在我的 StackPanel Childran 中执行拖放操作,
XAML
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="601" Width="637">
<StackPanel Name="sp" AllowDrop="True" Background="SkyBlue" PreviewMouseLeftButtonDown="sp_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="sp_PreviewMouseLeftButtonUp" PreviewMouseMove="sp_PreviewMouseMove"
DragEnter="sp_DragEnter" Drop="sp_Drop">
<Image Source="/Assets/Image1.jpg" Height="100" Width ="100"/>
<Image Source="/Assets/Image2.jpg" Height="100" Width ="100"/>
<Image Source="/Assets/Image3.jpg" Height="100" Width ="100"/>
<Image Source="/Assets/Image4.jpg" Height="100" Width ="100"/>
<Image Source="/Assets/Image5.jpg" Height="100" Width ="100"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
代码隐藏
Run Code Online (Sandbox Code Playgroud)public partial class Window1 : Window { public Window1() { InitializeComponent(); } private bool _isDown; private bool _isDragging; private Point _startPoint; private UIElement _realDragSource; private UIElement _dummyDragSource = new UIElement();
private void …
Run Code Online (Sandbox Code Playgroud) 我如何重新安排像Windows 8开始页面的列表框项目.我创建了一个能够拖放/重新排列其中的项目的示例.但我不知道如何实现像Windows 8开始屏幕那样的效果(就像拖动项目一样,然后放下鼠标).在我当前的实现中,当点击并拖动一个小盒子和光标时,整个项目都没有到来.我如何在WPF中实现这一点.我不是要问"为我做这件事".任何人请指导我实施相同的
注意:我是在触摸环境中创建的
我当前的实现是基于下面链接Stackoverflow链接中给出的答案
我有一个包含一些元素以及一个按钮的扩展器。当扩展器合拢时,我想在扩展器的标题中显示按钮,以便于访问。扩展器扩展后,我希望按钮成为扩展器内容的一部分。
当扩展器展开和折叠时,我可以使标题中的按钮淡入和淡出,但我想更进一步。我希望扩展器内容中的按钮在折叠时向上移动到标题中的位置,在扩展时向下移动到内容的位置。
通过使用blend并在两个按钮之间绘制一条线并将其转换为运动路径,我可以稍微接近一点。但是,使用这种方法很难获得精确的定位。我的一部分感觉就像画出了一条路,使事情变得有些复杂。我想要的只是让按钮A动画成为按钮B。
目前,我只关心按钮的位置。这两个按钮的大小不同,但是对按钮的宽度和高度进行动画处理则更加简单:)
这是我快速制作的一个示例程序,用于演示我正在使用的程序。主要是为了提供视觉布局。此示例中不包括渐变动画和路径动画。
<Window x:Class="test.MainWindow"
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"
xmlns:local="clr-namespace:test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="5" />
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="White" />
</Style>
</Window.Resources>
<Grid>
<Expander Margin="5" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Stretch" IsExpanded="True">
<Expander.Header>
<DockPanel Margin="5">
<TextBlock Margin="5" VerticalAlignment="Center">Hello World</TextBlock>
<Button Padding="5">Button</Button>
</DockPanel>
</Expander.Header>
<Expander.Content>
<StackPanel Orientation="Horizontal">
<Canvas Margin="5" Width="300" Background="Black" />
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type RadioButton}">
<Setter Property="Margin" Value="5" />
</Style>
</StackPanel.Resources>
<RadioButton IsChecked="True">Option 1</RadioButton>
<RadioButton>Option 2</RadioButton>
<RadioButton>Option 3</RadioButton>
<RadioButton>Option 4</RadioButton> …
Run Code Online (Sandbox Code Playgroud) 我是WPF和XAML的新手,所以我现在有ResourceDictionary
一个按钮:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ButtonProduct" TargetType="Button">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border"
CornerRadius="0"
BorderThickness="0"
Focusable="False"
BorderBrush="Transparent" Background="White">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#52b0ca"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在悬停时,按钮的颜色会发生变化,但是如何在淡入和淡出时进行更改,以便平滑过渡颜色?
我想以编程方式使用 ColorAnimation 来为单元格设置动画,但是我在执行时得到了这个 storyboard.Begin()
'System.Windows.Media.Animation.ColorAnimation' animation object cannot be used to animate property 'Background' because it is of incompatible type 'System.Windows.Media.Brush'.
Run Code Online (Sandbox Code Playgroud)
我已经定义了我ColorAnimation
的
var storyBoard = new Storyboard();
ColorAnimation colorAnimation = new ColorAnimation
{
From = Colors.Red,
To = Colors.CornflowerBlue,
Duration = TimeSpan.FromSeconds(1),
FillBehavior = FillBehavior.Stop
};
Run Code Online (Sandbox Code Playgroud)
和它的用法我做
if (column.UniqueName != "_ID")
{
var animation = animationMapping[column.UniqueName].Animation;
var storyboard = animationMapping[column.UniqueName].Storyboard;
Storyboard.SetTarget(animation, cell.Content as TextBlock);
//Storyboard.SetTargetProperty(animation,
// new PropertyPath((TextBlock.Foreground).Color"));
PropertyPath colorTargetPath = new PropertyPath(TextBlock.BackgroundProperty);
Storyboard.SetTargetProperty(animation, colorTargetPath);
storyboard.Begin();
}
Run Code Online (Sandbox Code Playgroud)
我必须将什么参数传递给新的 …
我试图找到一种通用方法来在每次更新 double 类型的属性时运行动画。
这必须是适用于所有双精度值的单一解决方案。这意味着我不想AttachedProperty
为每个UIElement
属性编写一个专有属性(一个为Opacity
,然后另一个为Height
)。
我想完成的一个伪示例:
<TextBlock x:Name="pageTitle" Text="title example" attached:AnimatedPropertyPath="(UIElement.Opacity)" Opacity="{Binding Path=TitleOpacity}" />
Run Code Online (Sandbox Code Playgroud)
附加属性应该侦听不透明度的任何变化,取消它并运行一个使其逐渐变化的动画。
我的问题:
我想避免使用 DataTriggers,因为它需要太多的 xaml。最好将其作为附加属性嵌入,就像上面的伪 xaml 一样。
我在.Net framework 4中使用WPF.
我创建了一个背景颜色为X的按钮.
我为按钮创建了一个样式,在IsMouseOver上启动ColorAnimation,将背景颜色更改为蓝色.
如果IsMouseOver为假(鼠标在按钮上没有记录器),我想将按钮背景恢复为颜色X.
示例代码:
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.5" Storyboard.TargetName="MyButton" Storyboard.TargetProperty="Fill.Color" To="Blue"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我正在尝试用故事板管理一个动画,它是一个闪烁的标签,并且实现了这个效果我已经使用了这个代码(动画我首选只在后面的代码中工作):我宣布我的故事板;
public partial class Example: Page
{
public Storyboard LabelStoryboard=new Storyboard();
}
Run Code Online (Sandbox Code Playgroud)
然后我发布动画:
private void FlashWinLabel(){
DoubleAnimation Flash= new DoubleAnimation();
Flash.Duration = TimeSpan.FromMilliseconds(400);
Flash.From = 1.0d;
Flash.To = 0.0d;
Flash.RepeatBehavior = RepeatBehavior.Forever;
WinLabel.BeginAnimation(Label.OpacityProperty, Flash);
SolidColorBrush myAnimatedBrush = new SolidColorBrush();
myAnimatedBrush.Color = Colors.White;
WinLabel.Foreground = myAnimatedBrush;
this.RegisterName("MyAnimatedBrush", myAnimatedBrush);
ColorAnimation LabelColorAnimation = new ColorAnimation();
LabelColorAnimation.To = Colors.GreenYellow;
LabelColorAnimation.Duration = TimeSpan.FromMilliseconds(200);
LabelColorAnimation.RepeatBehavior = RepeatBehavior.Forever;
LabelColorAnimation.AutoReverse = true;
Storyboard.SetTargetName(LabelColorAnimation, "MyAnimatedBrush");
Storyboard.SetTargetProperty(
LabelColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));
LabelStoryboard.Children.Add(LabelColorAnimation);
WinLabel.BeginStoryboard(LabelStoryboard);
}
Run Code Online (Sandbox Code Playgroud)
所有这一切都有效,直到这一点,当我试图通过按下屏幕上的按钮来停止故事板时出现问题:
private void StopStoryBoard()
{
LabelStoryboard.Stop();
} …
Run Code Online (Sandbox Code Playgroud) 我有一个ComboBox
,其Opacity
属性具有以下绑定:
Opacity="{Binding ElementName=stackPanel, Path=IsMouseOver, Converter={StaticResource mouseOverConverter}}"
Run Code Online (Sandbox Code Playgroud)
基本上,如果IsMouseOver
属性是真的,那就ComboBox
有了Opacity
为1,否则为0.4.
现在我将此动画应用于 ComboBox
:
private void AnimateComboBox()
{
DoubleAnimation da = new DoubleAnimation();
da.From = 0.4;
da.To = 1;
da.Duration = TimeSpan.FromSeconds(0.8);
da.AutoReverse = true;
ComboClassList.BeginAnimation(ComboBox.OpacityProperty, da);
}
Run Code Online (Sandbox Code Playgroud)
这很好,但之后绑定ComboBox
不再起作用.在Opacity
当我移动我的鼠标在不改变StackPanel
.为什么动画会破坏我的装订?Snoop说,绑定仍然存在,尽管它在Snoop中突出显示为红色.