我有一个 SurfaceRadioButton必须更改 ScatterView 的可见性 (散点图坐标)
首先,我所做的是改变对象的Visibility()
private void CoordinatesChecked(object sender, RoutedEventArgs e)
{
scatterViewCoordinates.Visibility = Visibility.Visible;
}
之后我修改了 XAML 代码,并将 ScatterView 的名称包含在 SurfaceRadioButton 的 Tag 值中。
<s:SurfaceRadioButton Name="Coordinates" Content="Coordinates"
Checked="CoordinatesChecked" Tag="scatterViewCoordinates" />
现在我尝试将 SurfaceRadioButton 中包含的 Tag 值转换为 ScatterView,然后调用 Visibility 方法。
private void CoordinatesChecked(object sender, RoutedEventArgs e)
{
string senderName = ((SurfaceRadioButton)sender).Tag.ToString();
((ScatterView)senderName).Visibility = Visibility.Hidden;
}
我得到了这个错误
无法将“string”类型的表达式转换为类型“ScatterView”
有什么解决这个问题的想法(如果这符合 MVVM 概念,我什至现在都不知道 :s)?
也欢迎提出建议。
最佳答案
为什么这不起作用应该很明显,您不能只将对象的名称强制转换为它本质上引用的对象。程序无法知道字符串 的含义。
只传递对象怎么样:
Tag="{Binding ElementName=scatterViewCoordinates}"
var view = (ScatterView)((SurfaceRadioButton)sender).Tag;
关于c# - 无法将类型 'string' 的表达式转换为类型 'ScatterView',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11470045/