1

Having application properties mapped like this:

<Application.Resources>
    <properties:Settings x:Key="Settings" />
</Application.Resources>

The goal is to bind font size setting MainWindowFontSize (int) to a selected value on combobox:

<ComboBox 
  SelectedValuePath="Content"
  SelectedValue="{Binding Default.MainWindowFontSize, Source={StaticResource Settings}}">
<ComboBoxItem>8</ComboBoxItem>
...
<ComboBoxItem>48</ComboBoxItem>
</ComboBox>

The issue with this is that it works only in one direction, from the setting into ComboBox, but any selection in the combo is not going back to the setting. Everything seems to work fine when I use a regular property for font size in a model...

Any suggestions on how to make the binding to work with a setting both ways?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Califf
  • 504
  • 6
  • 17
  • Have you taken a look at this? http://weblogs.asp.net/thomaslebrun/archive/2009/03/26/wpf-how-to-bind-a-control-to-a-property-defined-in-the-settings.aspx – d.moncada Nov 29 '12 at 19:27
  • The article refers to using ListBox... My question was about combobox... I can successfully make a TextBox work with settings, but not ComboBox... Thanks for looking it up though. – Califf Nov 29 '12 at 19:37
  • Yeah, after reading your post of the answer, I didn't realize it was control specific. – d.moncada Nov 29 '12 at 21:55
  • Creating a new settings instance to access a static property with the instance you actually want seems like a bad idea, i would recommend bindings in the form: `{Binding , Source={x:Static properties:Settings.Default}}` – H.B. Nov 30 '12 at 09:11
  • this is just a different way to express the same intention... I also came across a similar problem with combobox reported on msdn forums, claiming it's a bug in .net 4.5. (sorry, didn't save the link) There should not be any problems with the way I intended to use the Combobox. However, it just does not work as the other controls. – Califf Nov 30 '12 at 22:50

3 Answers3

2

It looks to be something new in .NET 4.5. I have found though that if you create binding in the code behind it works just fine. Like so:

    public MainWindow()
    {
        InitializeComponent();
        var binding = new Binding("Delay");
        binding.Source = Settings.Default;
        binding.Mode = BindingMode.TwoWay;
        BindingOperations.SetBinding(this.Combo, ComboBox.SelectedValueProperty, binding);
    }
1

Have you tried setting your binding's Mode to TwoWay?

<ComboBox 
  SelectedValuePath="Content"
  SelectedValue="{Binding Default.MainWindowFontSize, Source={StaticResource Settings}, Mode=TwoWay}">

You can try the UpdateSourceTrigger, also:

 <ComboBox 
  SelectedValuePath="Content"
  SelectedValue="{Binding Default.MainWindowFontSize, Source={StaticResource Settings}, Mode=TwoWay}, UpdateSourceTrigger=PropertyChanged">
Arthur Nunes
  • 6,718
  • 7
  • 33
  • 46
  • yes, no difference... Also I think this is default value for Mode – Califf Nov 29 '12 at 19:15
  • Maybe the UpdateSourceTrigger, then. See http://stackoverflow.com/questions/1187294/wpf-combobox-update-source-question and http://stackoverflow.com/questions/4770912/how-to-use-update-source-trigger-on-wpf-combobox-which-is-editable – Arthur Nunes Nov 29 '12 at 19:23
  • Unfortunately, UpdateSourceTrigger=PropertyChanged didn't help either... This works great though: The problem clearly seems to be with the combobox... – Califf Nov 29 '12 at 19:27
  • `TwoWay` is the default, see the dependency property information section in [the docs](http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvalue.aspx). – H.B. Nov 30 '12 at 09:17
1

Found this workaround:

<ComboBox ...  SelectionChanged="MainWndFontSizeSelectionChanged" ...>

The event handler:

private void MainWndFontSizeSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var cb = (ComboBox)sender;
    int newSize = 0;
    if (Int32.TryParse(cb.SelectedValue.ToString(), out newSize) == true)
    {
        WpfApplication1.Properties.Settings.Default.MainWindowFontSize = newSize;
    }
}

Ugly, but works... Hoping for a better solution to come up...

This post provides more insight into the issue as it appears:LINK

It does not work the same way in .NET4.5 as in the previous versions.

Community
  • 1
  • 1
Califf
  • 504
  • 6
  • 17
  • The link should have nothing to do with this, you do not bind to a static property. – H.B. Nov 30 '12 at 09:14
  • Source={StaticResource Settings} <<<--- means its static, no? – Califf Nov 30 '12 at 22:47
  • That is not the a property it's the source object, also `StaticResource` has nothing to do with [what is usually meant by `static`](http://msdn.microsoft.com/en-us/library/98f28cdx.aspx). – H.B. Dec 01 '12 at 06:44