通过 Javascript 访问 Xaml 元素属性

发布于 2024-11-05 20:00:49 字数 1272 浏览 0 评论 0原文

(再次,一个 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,将“elij 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 事件重置一切。

(Again, a Xaml question. Our teacher isn't very useful unfortunately...)

I have following elements in a xaml file:

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

I also have 200 line elements, containing data, like this:

<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" />

Now, the idea is that, in the onMouseEnter function (in a javascript file), I extract data from the "Tag" attribute and put it as text in the textblocks. In this example:

0.0427409|e l i j k|1

So, I have to put '0.0427409' in the InformationGainTextBlock, 'e l i j k' in the NGramTextBlock and '1' in the PositionTextBlock. I also have to change the line colour.

How am I able to do this? I think I've got the pseudocode about right, but not the exact implementation:

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";
}

The onMouseLeave event resets everything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2024-11-12 20:00:49

像这样的东西应该有效:

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];
}

你所拥有的非常接近:)

Something like this should work:

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];
}

What you had was very close :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文