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

标签 c# wpf xaml mvvm pixelsense

我有一个 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/

相关文章:

c# - 从另一个项目中删除一组项目的最快方法是什么?

c# - 是否可以将 WPF RichTextBox 中的文本设为 "zoom"?

c# - MessageBox.Show 导致无法拾取光标的变化

设置某些值后,C#/WPF 位图保持白色(使用 LockBits)

c# - 如何删除 Xamarin.Forms 中的 (Android) 应用程序标题栏?

c# - 如何在本地网络的一台计算机上运行业务逻辑,在另一台计算机上运行GUI?

c# - 使用linq删除gridview中的行

c# - 具有在 C# 中可见的算术运算符的 F# 结构

c# - 轻推 WPF 窗口

c# - 提取对象类型以用作通用 <T>