Showing posts with label binding. Show all posts
Showing posts with label binding. Show all posts

Binding to a Settings file

Something that I find really cool is that you can bind directly to a user settings file in wpf. In the example below I've bound a toggle button to a user setting in a settings file. Toggling the button will update the settings file with 0 code!

<ToggleButton x:Name="btnNetworkSpeed" Margin="5,0,0,0" FlowDirection="LeftToRight" IsChecked="{Binding Source={x:Static InfrastructureProperties:Settings.Default}, Path=IsSlowConnectionSpeed, Mode=TwoWay}" FontSize="9"/>

The trick to making this work is to change the settings file access modifier to Public. To do this go into the settings file and at the top of the designer there is a drop down. What this does under the covers is change the compile tool from 'SettingsSingleFileGenerator' to 'PublicSettingsSingleFileGenerator'

If you want the setting to be persisted on the closing event of the application add this line and you're done.

protected override void OnExit(ExitEventArgs e)
{
Modules.Infrastructure.Properties.Settings.Default.Save();
...
}

Bug in ItemsControl with nested bindings

I came across a weird bug in the wpf ItemsControl today. I found that when I added a converter to the the ItemTemplate it killed the binding on the ItemTemplate.

After a lot of searching I found a few other people with the same problem but not many solutions. One workaround that seems to have been successful is to move the template into a resource. This didn't work for me but might be worth a try if you come across the same thing.