Silverlight 4 Databinding with Styles
In my silverlight application I have a series of textboxes on a page that all bind the same way. In the codebehind I am setting the datacontext of each one. Has anyone incorperated this common binding functionality using styles?
Example in my page.xaml
<TextBox Name="txtTest" Style="{StaticResource ItemTextBoxInt}"/>
in my Styles.xaml
<Resource Dictionary>
<Style x:Key="ItemTextBoxInt" TargetType="TextBox">
<Setter Property="Text" Value="{Binding Amount, Mode=TwoWay, StringFormat=\{0:n0\}}"/>
</Style>
this throws an error in the App.xaml.cs on startup
{System.Windows.Markup.XamlParseException: Set property '' threw an exception. [Line: 9 Position: 36] ---> System.NotSupportedException: Cannot set read-only property ''. at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value) at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue) --- End of inner exception stack trace --- at System.Windows.A开发者_JAVA技巧pplication.LoadComponent(Object component, Uri resourceLocator) at STARS_Silverlight.App.InitializeComponent() at STARS_Silverlight.App..ctor()}
Anyone know if this is possible and if so the correct way to accomplish this?
-Andrew
Unfortunately SL4 still does not support bindings in styles. Workaround: http://blogs.msdn.com/b/delay/archive/2009/11/02/as-the-platform-evolves-so-do-the-workarounds-better-settervaluebindinghelper-makes-silverlight-setters-better-er.aspx
Verify the Amount
property has a publicly exposed setter.
精彩评论