我只是想將參數傳遞給控件。但它引發錯誤「輸入字符串格式不正確」。爲什麼* *wpf附屬屬性不起作用
的XAML
<Views:SomeView SecurityId="abc"></Views:SomeView>
型號:
class Data
{
public string Case { get; set; }
public Data(int _input)
{
if (_input==1)
{
Case = "First";
}
else
{
Case = "Second";
}
}
}
視圖模型:
class DataViewModel
{
public string GetData
{
get
{
return D.Case;
}
set
{
D.Case = value;
}
}
public Data D;
public DataViewModel(string i)
{
D = new Data(Convert.ToInt16(i));
}
}
主窗口
public partial class SomeView : UserControl
{
public string SecurityId
{
get
{
return (string)GetValue(SecurityIdProperty);
}
set { SetValue(SecurityIdProperty, value); }
}
public static readonly DependencyProperty
SecurityIdProperty =
DependencyProperty.Register("SecurityId",
typeof(string), typeof(SomeView),
new PropertyMetadata(""));
public SomeView()
{
DataContext = new DataViewModel(SecurityId);
InitializeComponent();
}
}
''abc「'不能用'Convert.ToInt16(i)'分析。 – Sinatr
我知道,它的錯誤。但是DataViewModel中的i值的主要問題在於「」。例如,當我改變// D =新數據(Convert.ToInt16(i));與Debug.WriteLine(i);.它正在打印「」,而不是「abc」 – A191919