4
A
回答
4
您將需要使用Storyboard
和動畫Line.X2
和Line.Y2
屬性。看看這是否適合你。
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas Name="myCanvas">
<Button Canvas.Left="248" Canvas.Top="222" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
</Canvas>
</Window>
按鈕單擊事件
private void button1_Click(object sender, RoutedEventArgs e)
{
Line line = new Line();
myCanvas.Children.Add(line);
line.Stroke = Brushes.Red;
line.StrokeThickness = 2;
line.X1 = 0;
line.Y1 = 0;
Storyboard sb = new Storyboard();
DoubleAnimation da = new DoubleAnimation(line.Y2 , 100, new Duration(new TimeSpan(0, 0, 1)));
DoubleAnimation da1 = new DoubleAnimation(line.X2, 100, new Duration(new TimeSpan(0, 0, 1)));
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.Y2)"));
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X2)"));
sb.Children.Add(da);
sb.Children.Add(da1);
line.BeginStoryboard(sb);
}
12
我有一個正在運行的示例,它使用MVVM模式並在ListBox
內創建Canvas
作爲ItemsPanel
的行。
我實際上是爲this question做的,但OP的那種消失了,從來沒有和我聯繫過。
這就是它看起來像在我的電腦:
它的主要部分是這樣的:
<ListBox ItemsSource="{Binding}" x:Name="lst" Height="500" Width="500">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style TargetType="Control">
<Setter Property="Opacity" Value="0"/>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Line X1="{Binding X1}" Y1="{Binding Y1}"
X2="{Binding X2}" Y2="{Binding Y2}"
StrokeThickness="{Binding Thickness}"
Opacity="{Binding Opacity}"
x:Name="Line">
<Line.Stroke>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="{Binding Color1}" Offset="0"/>
<GradientStop Color="{Binding Color2}" Offset="1"/>
</LinearGradientBrush>
</Line.Stroke>
</Line>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Effect" TargetName="Line">
<Setter.Value>
<DropShadowEffect Color="CornflowerBlue" ShadowDepth="3" BlurRadius="10"/>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
視圖模型:
public class LineViewModel : INotifyPropertyChanged
{
#region Timer-based Animation
private System.Threading.Timer Timer;
private static Random Rnd = new Random();
private bool _animate;
public bool Animate
{
get { return _animate; }
set
{
_animate = value;
NotifyPropertyChanged("Animate");
if (value)
StartTimer();
else
StopTimer();
}
}
private int _animationSpeed = 1;
public int AnimationSpeed
{
get { return _animationSpeed; }
set
{
_animationSpeed = value;
NotifyPropertyChanged("AnimationSpeed");
if (Timer != null)
Timer.Change(0, 100/value);
}
}
private static readonly List<int> _animationSpeeds = new List<int>{1,2,3,4,5};
public List<int> AnimationSpeeds
{
get { return _animationSpeeds; }
}
public void StartTimer()
{
StopTimer();
Timer = new Timer(x => Timer_Tick(), null, 0, 100/AnimationSpeed);
}
public void StopTimer()
{
if (Timer != null)
{
Timer.Dispose();
Timer = null;
}
}
private void Timer_Tick()
{
X1 = X1 + Rnd.Next(-2, 3);
Y1 = Y1 + Rnd.Next(-2, 3);
X2 = X2 + Rnd.Next(-2, 3);
Y2 = Y2 + Rnd.Next(-2, 3);
}
#endregion
#region Coordinates
private double _x1;
public double X1
{
get { return _x1; }
set
{
_x1 = value;
NotifyPropertyChanged("X1");
}
}
private double _y1;
public double Y1
{
get { return _y1; }
set
{
_y1 = value;
NotifyPropertyChanged("Y1");
}
}
private double _x2;
public double X2
{
get { return _x2; }
set
{
_x2 = value;
NotifyPropertyChanged("X2");
}
}
private double _y2;
public double Y2
{
get { return _y2; }
set
{
_y2 = value;
NotifyPropertyChanged("Y2");
}
}
#endregion
#region Other Properties
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChanged("Name");
}
}
private double _thickness;
public double Thickness
{
get { return _thickness; }
set
{
_thickness = value;
NotifyPropertyChanged("Thickness");
}
}
public Color Color1 { get; set; }
public Color Color2 { get; set; }
private double _opacity = 1;
public double Opacity
{
get { return _opacity; }
set
{
_opacity = value;
NotifyPropertyChanged("Opacity");
}
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}));
}
#endregion
}
編輯:源代碼現在在GitHub
+0
我無法下載源文件。你能再次提供正確的鏈接嗎? – RobinAtTech 2014-03-30 22:35:21
+0
https://github.com/High-Core/WPFSamples/tree/master/src/WPFSamples/Samples/LinesEditor – 2015-12-23 21:16:32
相關問題
- 1. 如何在畫面上方製作畫布線條畫?
- 2. 製作一個事件變量在畫布上畫一條線
- 3. 在畫布上畫一條二維線
- 4. Chart.js如何在條形圖的畫布上添加一條線
- 5. touchmove在畫布上畫兩條線而不是一條線
- 6. 如何縮放用於在畫布上製作線條/曲線的所有點?
- 7. 如何在html5畫布中爲自定義線條製作線條寬度
- 8. 如何在畫布上製作生長線動畫android
- 9. 畫布 - 如何繪製一條正在增長的路線
- 10. 在創建片段時通過畫布在畫布上繪製一條線
- 11. HTML5畫布 - 如何在圖像背景上繪製線條?
- 12. java:如何在畫布上繪製線條
- 13. 如何在實時畫布上用像素繪製線條
- 14. 在畫布上同時畫兩條線
- 15. 用畫布繪製線條
- 16. 從一個畫布繪製線條到另一個畫布
- 17. 在畫布上用另一條虛線畫線
- 18. 如何在LinearLayout上畫一條線?
- 19. 如何在畫布上繪製畫布
- 20. 如何在畫布上選擇並拖動一條線?
- 21. 如何在HTML5畫布上正確繪製多條不同寬度的線條?
- 22. 如何在WPF中的1像素厚的畫布上繪製線條
- 23. 如何在Android中使用畫布繪製線條?
- 24. 在畫布上繪製GOOD LOOKING(如Flash)線條(HTML5) - 可能嗎?
- 25. 如何在Objective C中繪製線條的動畫?
- 26. Android:如何檢測由畫布繪製的線條中的點?
- 27. 我如何在自己的佈局上繪製一條線?
- 28. 如何在畫布上繪製另一個線程?
- 29. 如何讓用戶使用WPF中的畫布繪製線條
- 30. 在HTML5畫布上點擊兩次鼠標畫一條線?
我認爲,不要單獨爲'X2'和'Y2'屬性設置動畫,我認爲從'Line'類擴展以展示'Angle'和'Radius'屬性並將其設爲動畫將是一個更好的主意。 – 2013-03-18 04:04:54
@JeffMercado你是對的,這只是給OP提供一個方向的一個簡單例子。 – 2013-03-18 04:06:19
謝謝!這是短暫而甜蜜的。正是我需要的。我在你的債務。 – UnskilledBuild 2013-03-18 05:50:21