我有一个绑定到ViewModel的TextBox。 TextBox的TextWrapping属性绑定到我的View Model上名为DocViewerWrapText的属性。这是我的财产 - 忽略关于打开和关闭scollbar的一些内容。
public string DocViewerWrapText
{
get { return _docViewerWrapText; }
set
{
_docViewerWrapText = value;
if (_docViewerWrapText == "Wrap")
ShowDocViewerHorizontalScrollBar = "Disabled";
else ShowDocViewerHorizontalScrollBar = "Auto";
NotifyPropertyChanged("ShowDocViewerHorizontalScrollBar");
NotifyPropertyChanged("DocViewerWrapText");
}
}
这段代码实际上运行得很好,但它会抛出First Chance Exception,如下所示:
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.
我尝试将我的Property转换为布尔值,但我得到了相同类型的错误。我还查看了TextWrapping枚举的实际枚举。值为1和2,所以我不知道如何在这里使用布尔值。
有人可以告诉我在XAML中绑定到枚举的正确方法吗?
谢谢,
-Scott
答案 0 :(得分:1)
我发现了问题。我需要更改propety getter / setter以返回正确的Enum类型。令我困惑的问题是布尔错误。事实证明,这是来自我在表单上的按钮来切换文本包装值。我将它绑定到同一属性,因此无法将Enum值转换为Boolean。
现在我只需要弄清楚如何转换价值。
-Scott