Tuesday, October 20, 2009

WPF Binding app.config settings in XAML

I tried several different syntaxes for exposing an app.config MySettings property (VB.Net, not C#) for databinding in the XAML. I was met with several compile and/or designer load errors like:

  1. could not create an instance of type StaticExtension
  2. ‘MemberReferenceSerializer’ ValueSerializer cannot convert from ‘System.String’
  3. A TwoWay or OneWayToSource binding cannot work on the read-only property of ‘TargetShiftCount’ of type PacingChart.MySettings

The databinding syntax that worked was:

   1: <Window x:Class="Window1"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     xmlns:p="clr-namespace:PacingChart"       
   5:     Name="window1"
   6:     Title="Pacer" Height="300" Width="300">
   7:     <Grid>
   8:         <TextBox Text="{Binding Source={x:Static p:MySettings.Default}, Path=TargetShiftCount, Mode=OneWay}" Name="txtShiftTarget" Margin="126,111,32,128"   />
   9:     </Grid>
  10: </Window>

On code snippet line 3, note the clr-namespace:<project root namespace> where <project root namespace> is PacingChart in this example. It did not work in my project with clr-namespace:PacingChart.Properties as some examples suggested. Those examples also indicated that the binding source should be "{Binding Source={x:Static p:Settings.Default}, Path=TargetShiftCount}". Using p:Settings.Default, not the p:MySettings.Default which worked in my example. Also note the lack of the Mode=OneWay attribute which caused the third error message listed above.

Most of the examples I attempted to follow were from people who asked the question about the correct VB.Net syntax, but the people responding were documenting the C# syntax. The Properties.Settings (in C#) and My.Settings (in Visual Basic) are helper classes. It is important to understand the syntax differences. In the VB.Net codebehind, you would access the settings using the syntax My.Settings.TargetShiftCount where in the XAML, you would use the MySettings class.

For completeness, I created the app.config property settings by double-clicking the “My Project” entry in the Solution Explorer and switching to the “Settings” tab, which created the following snippet in the app.config file.

   1: <applicationSettings>
   2:     <PacingChart.MySettings>
   3:         <setting name="TargetShiftCount" serializeAs="String">
   4:             <value>200</value>
   5:         </setting>
   6:     </PacingChart.MySettings>
   7: </applicationSettings>


Aynkaran said...

I also had the problem. Using 'MySettings' solved it. Thank you

Yuri Bondarenko said...

Here is the clear solution for this problem:

Text="{Binding Source={x:Static p:Settings.Default}, Path=ServerName, Mode=OneWay}"

Annie Calvert said...

Data binding links a data layer with graphical controls and enables data independency of its presentation. Data binding is broadly used in WinForms applications, while in WPF applications it is practically the only data presentation method. Correct organization of data binding is the foundation of well-built applications.more detail visit dapfor. com