热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

无法将'string'类型的表达式转换为'ScatterView'类型-Cannotcastexpressionoftype'string'totype'ScatterView'

IhaveaSurfaceRadioButtonthathastochangetheVisibilityofScatterView(scatterViewCoordinates

I have a SurfaceRadioButton that has to change the Visibility of ScatterView (scatterViewCoordinates)

我有一个SurfaceRadioButton,它必须改变ScatterView的可见性(scatterViewCoordinates)

First, what I have done was to change the Visibility of the object ()

首先,我所做的是改变对象的可见性()

private void CoordinatesChecked(object sender, RoutedEventArgs e)
{
    scatterViewCoordinates.Visibility = Visibility.Visible;
}

After that I modified the XAML code, and I included the name of the ScatterView in the Tag value of the SurfaceRadioButton.

之后我修改了XAML代码,并在SurfaceRadioButton的Tag值中包含了ScatterView的名称。


Now I was trying to cast the Tag value contained in the SurfaceRadioButton to a ScatterView, and after that to call the Visibility method.

现在我试图将SurfaceRadioButton中包含的Tag值转换为ScatterView,然后调用Visibility方法。

private void CoordinatesChecked(object sender, RoutedEventArgs e)
{
    string senderName = ((SurfaceRadioButton)sender).Tag.ToString();
    ((ScatterView)senderName).Visibility = Visibility.Hidden;
}

And I get this error

我得到了这个错误

Cannot cast expression of type 'string' to type 'ScatterView'

无法将'string'类型的表达式转换为'ScatterView'类型

Any ideas to resolve this problem (I don't even now if this respect the MVVM concept :s) ?

任何解决这个问题的想法(如果这尊重MVVM概念,我现在甚至都没有)?

Suggestions are welcomed too.

建议也受到欢迎。

2 个解决方案

#1


4  

It should be obvious why this does not work, you cannot just cast the name of an object to the object it intrinsically references. There is no way for the program to know what the string means.

很明显为什么这不起作用,你不能只是将对象的名称强制转换为它本身引用的对象。程序无法知道字符串的含义。

How about just passing the object:

如何传递对象:

Tag="{Binding ElementName=scatterViewCoordinates}"
var view = (ScatterView)((SurfaceRadioButton)sender).Tag;

#2


1  

You are trying to cast "senderName", which is a string, to ScatterView, just like the error says. You will need to find ScatterView based on name and than set its visibility.

您正在尝试将“senderName”(这是一个字符串)转换为ScatterView,就像错误所说的那样。您需要根据名称查找ScatterView,然后设置其可见性。


推荐阅读
author-avatar
Binggo89
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有