Scott
Scott

Reputation: 904

Silverlight - Binding to Enum properties

I have a TextBox that is bound to my ViewModel. The TextWrapping property of the TextBox is bound to a property on my View Model called DocViewerWrapText. Here is my Property - ignore the bits about turning on and off the scollbar.

 public string DocViewerWrapText
{
  get { return _docViewerWrapText; }
  set
  {
    _docViewerWrapText = value;

    if (_docViewerWrapText == "Wrap")
      ShowDocViewerHorizontalScrollBar = "Disabled";
    else ShowDocViewerHorizontalScrollBar = "Auto";
    NotifyPropertyChanged("ShowDocViewerHorizontalScrollBar");
    NotifyPropertyChanged("DocViewerWrapText");
  }
}

This code actually works just fine, but it throws a First Chance Exception as follows:

System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value 'NoWrap' (type 'System.String'); BindingExpression: Path='DocViewerWrapText' DataItem='UnityEca.ViewModels.HomeViewModel' (HashCode=41697354); target element is 'Telerik.Windows.Controls.RadToggleButton' (Name='docViewerWrapText'); target property is 'IsChecked' (type 'System.Nullable`1[System.Boolean]').. System.FormatException: String was not recognized as a valid Boolean.

I've tried converting my Property to a boolean, but I get the same type of error. I've also looked at the actual enum for the TextWrapping enum. The values are 1 and 2, so I don't see how I could use a boolean here anyway.

Can someone tell me the proper way to bind to enums such as this in XAML?

Thanks,

-Scott

Upvotes: 0

Views: 604

Answers (1)

Scott
Scott

Reputation: 904

I found the problem. I needed to change the propety getter/setter to return the correct Enum type. The issue that was confusing me was the boolean error. It turns out that was coming from a pushbutton I had on the form to toggle the text wrapping value. I have it bound to the same property, so it was not able to convert from Enum value to Boolean.

Now I just need to figure out how to convert the values.

-Scott

Upvotes: 1

Related Questions