2013-02-03 129 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