通过Javascript访问Xaml元素属性

时间:2011-05-07 17:25:18

标签: javascript silverlight xaml

(同样,一个Xaml问题。不幸的是,我们的老师不是很有用......)

我在xaml文件中有以下元素:

<TextBlock Style="{StaticResource TitleText}" x:Name="InformationGainTextBlock" />
<TextBlock Style="{StaticResource TitleText}" x:Name="NGramTextBlock" />
<TextBlock Style="{StaticResource TitleText}" x:Name="PositionTextBlock" />

我还有200个包含数据的行元素,如下所示:

<Line Name="Data0" Stroke="Maroon" StrokeThickness="1" X1="154" Y1="123" X2="154" Y2="549" MouseEnter="onMouseEnter" MouseLeave="onMouseLeave" Tag="0.0427409|e l i j k|1" />

现在,我们的想法是,在onMouseEnter函数(在javascript文件中)中,我从“Tag”属性中提取数据并将其作为文本放在文本块中。在这个例子中:

0.0427409|e l i j k|1

所以,我必须将'0.0427409'放在InformationGainTextBlock中,'e l i j k'放在NGramTextBlock中,将'1'放在PositionTextBlock中。我还必须改变线条颜色。

我怎么能这样做?我想我的伪代码是正确的,但不是确切的实现:

onMouseEnter(sender, args) {

var data = sender.getAttribute("Tag").Text;
var array[] = data.Split("|");

sender.getElementByName("InformationGainTextBlock").text = array[0];
sender.getElementByName("NGramTextBlock").text = array[1];
sender.getElementByName("PositionTextBlock").text = array[2];
sender.getAttribute("Stroke").text = "Red";
}

onMouseLeave事件会重置所有内容。

1 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

function onMouseEnter(sender, mouseEventArgs) {
  var text = sender["Tag"];
  var array = new Array();
  array = text.split("|");
  sender["Stroke"] = "Red";
  sender.findName("InformationGainTextBlock").text = array[0];
  sender.findName("NGramTextBlock").text = array[1];
  sender.findName("PositionTextBlock").text = array[2];
}

你所拥有的非常接近:)