C# 我如何投一个“球”;“框架元素”;至“a”;“线型”;在WPF中?
我试图使用“foreach”在“Canvas”上迭代一系列“线型”。 在“foreach”的主体中,我想更改每个“行”的“X1”、“Y1”、“X2”、“Y2”坐标 我在下面创建了一个简单的示例。 下面是XAML和C#代码的小片段,在画布上创建了3行C# 我如何投一个“球”;“框架元素”;至“a”;“线型”;在WPF中?,c#,wpf,canvas,C#,Wpf,Canvas,我试图使用“foreach”在“Canvas”上迭代一系列“线型”。 在“foreach”的主体中,我想更改每个“行”的“X1”、“Y1”、“X2”、“Y2”坐标 我在下面创建了一个简单的示例。 下面是XAML和C#代码的小片段,在画布上创建了3行 <Window x:Class="TestCase.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xm
<Window x:Class="TestCase.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Canvas Height="500" Width="500" Name="My_Canvas">
<Line StrokeThickness="10" Stroke="Black" X1="100" Y1="100" X2="200" Y2="100"
Tag="Line #1"/>
<Line StrokeThickness="10" Stroke="Black" X1="100" Y1="200" X2="200" Y2="200"
Tag="Line #2"/>
<Line StrokeThickness="10" Stroke="Black" X1="100" Y1="300" X2="200" Y2="300"
Tag="Line #3"/>
</Canvas>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestCase
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
foreach (FrameworkElement Hold_Element in My_Canvas.Children)
{
MessageBox.Show("Tag = " + Hold_Element.Tag);
//(Line)Hold_Element.X1 = (Line)Hold_Element.X1 + 90;
}
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间测试用例
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
foreach(FrameworkElement在My_Canvas.Children中保留_元素)
{
MessageBox.Show(“Tag=“+Hold\u Element.Tag”);
//(行)Hold_Element.X1=(行)Hold_Element.X1+90;
}
}
}
}
C#代码在各行之间迭代,并在消息框中正确显示每个“行”的“标记”
注释掉的那一行是我要应用的代码
我想将每条“线”的X1坐标向右移动90个单位。
如何使此注释行有效?
我找不到正确的演员。。。
不知何故,我必须将“FrameworkElement”转换为“Line Shape”,并在“Line Shape”上设置“X1”坐标
谢谢,
吉姆你基本上是对的——只需在演员阵容周围加上括号:
((Line)Hold_Element).X1 = ((Line)Hold_Element).X1 + 90;
或者,请注意,您可以使用OfType
Linq扩展方法在foreach
语句中指定类型:
foreach (Line Hold_Element in My_Canvas.Children)
{
MessageBox.Show("Tag = " + Hold_Element.Tag.OfType<Line>());
Hold_Element.X1 = Hold_Element.X1 + 90;
}
foreach(mycanvas.Children中的Line Hold\u元素)
{
Show(“Tag=“+Hold_Element.Tag.OfType());
Hold_Element.X1=Hold_Element.X1+90;
}
第二种方法更可取,因为它只返回正确类型的元素(因此您不会冒运行时“无效强制转换”异常的风险)。类型强制转换不正确。将对象包装在括号中以访问其属性(在本例中为X1) 替换
(Line)Hold_Element.X1 = (Line)Hold_Element.X1 + 90;
到
谢谢McGarnagle。我使用了((行)Hold_元素).X1=((行)Hold_元素).X1+90;两种方法都像冠军一样有效,但我需要使用更长的铸造方法。是的…((线)Hold_元素)。X1=((线)Hold_元素)。X1+90;。。。这正是我需要的。
((Line)Hold_Element).X1 = ((Line)Hold_Element).X1 + 90;