Search code examples
xamlvisual-studio-2012windows-phone-8

Cannot convert String to String


I have a textBox in the xaml.
But I get this message when navigating to other page.
The textBox is dynamically monitored when input, focus and lost focus.
(To detect if there contain text or not and change the text color.)
Everything's working fine, I don't know what do the exception do.

This is the 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>

This is the exception thrown:

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().

How do I get rid of it?


Solution

  • You're trying to bind a text from the localized resources by using a two-ways binding. It cannot work, because these resources are read-only.

    I believe you're just trying to set the initial value of your textbox. Therefore, you should bind to a property of your own, and initialize it using the resources.

    First, create the property in your viewmodel:

    public string SearchBoxColorText { get; set; }
    

    Initialize the property somewhere in your code (in the class constructor, in the OnNavigatedTo event, whatever suits your workflow):

    this.SearchBoxColorText = LocalizedStrings.StaticlocalizedResources.Desc_InputKey;
    

    Then bind the textbox to this property:

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