2013-02-03 132 views
3

我在xaml中有一个文本框。
但是,在导航到其他页面时我收到此消息。
当输入,焦点和失去焦点时,textBox被动态监视。
(要检测是否包含文本,并更改​​文字颜色。)
一切工作正常,我不知道什么是例外。
无法将字符串转换为字符串

这是文本框XAML

<TextBox Name="SearchBox" TextChanged="OnTextChanged" Height="72" InputScope="Search" GotFocus="OnGotFocus" KeyDown="OnKeyDown" LostFocus="OnLostFocus" 
    Text="{Binding LocalizedResources.Desc_InputKey, Mode=TwoWay, Source={StaticResource LocalizedStrings}}" > 
    <TextBox.Foreground> 
     <SolidColorBrush x:Name="SearhBoxColor" Color="{StaticResource PhoneTextBoxReadOnlyColor}"/> 
    </TextBox.Foreground> 
</TextBox> 

这是抛出的异常:

System.Windows.Data Error: ConvertBack cannot convert value 'hhh' (type 'System.String'). BindingExpression: Path='LocalizedResources.Desc_InputKey' DataItem='MyProject.LocalizedStrings' (HashCode=15848707); target element is 'System.Windows.Controls.TextBox' (Name='SearchBox'); target property is 'Text' (type 'System.String').. 
    System.ArgumentException: Property set method not found. 
    at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) 
    at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) 
    at System.Windows.CLRPropertyListener.set_Value(Object value) 
    at System.Windows.PropertyAccessPathStep.set_Value(Object value) 
    at System.Windows.Data.BindingExpression.UpdateValue(). 

如何摆脱它?

+2

什么是'LocalizedStrings'?它是如何定义的? – Blachshma

回答

1

您试图通过使用双向绑定来绑定来自本地化资源的文本。它无法工作,因为这些资源是只读的。

我相信你只是试图设置你的文本框的初始值。因此,您应该绑定到您自己的属性,并使用资源对其进行初始化。

首先,在您的视图模型创建属性:

public string SearchBoxColorText { get; set; } 

在你的代码的某个地方初始化属性(在类的构造函数,在OnNavigatedTo情况下,任何适合你的工作流程):

this.SearchBoxColorText = LocalizedStrings.StaticlocalizedResources.Desc_InputKey; 

然后将文本框绑定到此属性:

<TextBox Name="SearchBox" Text="{Binding SearchBoxColorText}" > 
+0

感谢您指出:) – user1510539