簡體   English   中英

無法將'string'類型的表達式轉換為'ScatterView'類型

[英]Cannot cast expression of type 'string' to type 'ScatterView'

我有一個SurfaceRadioButton ,它必須改變ScatterView可見性 (scatterViewCoordinates)

首先,我所做的是改變對象的可見性()

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

之后我修改了XAML代碼,並在SurfaceRadioButton的Tag值中包含了ScatterView的名稱。

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

我得到了這個錯誤

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

任何解決這個問題的想法(如果這尊重MVVM概念,我現在甚至都沒有)?

也歡迎提出建議。

很明顯為什么這不起作用,你不能只是將對象的名稱強制轉換為它本身引用的對象。 程序無法知道字符串的含義

如何傳遞對象:

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

您正在嘗試將“senderName”(這是一個字符串)轉換為ScatterView,就像錯誤所說的那樣。 您需要根據名稱查找ScatterView,然后設置其可見性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM