Cannot convert String to String

I have a textbox in xaml.
But I get this message when navigating to another page.
The TextBox is dynamically controlled on input, focus and focus loss.
(To detect if there is text or not and change the color of the text.)
Everything works fine, I don't know what the exception is doing.

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 an exception:

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 can I get rid of it?

+3


source to share


1 answer


You are trying to bind text to localized resources using two-way binding. It cannot work because these resources are read-only.

I believe you are just trying to set the initial value of your textbox. Hence, you must bind to your own property and initialize it with resources.

First, create a property in your view model:

public string SearchBoxColorText { get; set; }

      



Initialize the property somewhere in your code (in the constructor of the class, in the event OnNavigatedTo

, regardless of your workflow):

this.SearchBoxColorText = LocalizedStrings.StaticlocalizedResources.Desc_InputKey;

      

Then bind the textbox to this property:

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

      

+1


source







All Articles