17

I have a Window with the following namespace

xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"

that contains a textbox

<TextBox Text="{Binding Path=Price, Mode=TwoWay, StringFormat='C',
                 ConverterCulture={x:Static sysglb:CultureInfo.CurrentCulture}}"
                MaxLines="1" TextAlignment="Right"/>

as per Gusdor's reply to StringFormat Localization issues in wpf which was working fine but now Visual Studio (2013) is giving me an "Invalid Markup" - The member "CurrentCulture" is not recognized or is not accessible error.

The Intellisense recognises and prompts sysglb:CultureInfo.CurrentCulture but as soon as I move away from the textbox I get the error.

Could some kind soul advise why this is happening and what I do to fix it? Also how the XAML editor manages to recognize sysglb:CultureInfo.CurrentCulture yet the markup doesn't?

Cheers Geoff

Community
  • 1
  • 1
Geoff Scott
  • 897
  • 1
  • 10
  • 17
  • You know what. I've just upgraded to VS 2015 and have started getting this error. How ironic! Did you fix it? – Gusdor Oct 12 '15 at 12:28
  • 1
    @Gusdor That's not irony – Dai Oct 26 '15 at 00:46
  • Changing the project target framework to .NET Framework 4.6 or higher solves the issue for VS2013 & VS2015. – kmb Jan 26 '16 at 15:19
  • This is a bug in VS2015 as far as I can tell. I'm using ConverterCulture in the same way you are, and targeting .NET 4.5. The designer crashes, but the project still builds and runs. As a workaround, temporarily remove the ConverterCulture to get the designer working again. – p10ben Apr 17 '17 at 11:04

3 Answers3

8

Can't remember where I got this from but it works

using System.Globalization;
using System.Windows.Data;

namespace SomeNamespace
{
    /// <summary>
    /// This class is a fudge because
    /// 
    ///         xmlns:sysglb="clr-namespace:System.Globalization;assembly=mscorlib"
    ///         
    ///         <TextBox Grid.Row="2" Grid.Column="1" 
    ///              Text="{Binding Path=SelectedSupporterCategory.Price, Mode=TwoWay, StringFormat='C',
    ///              ConverterCulture={x:Static sysglb:CultureInfo.CurrentCulture}}" 
    ///              UseLayoutRounding="True" MaxWidth="100" HorizontalAlignment="Left" MinWidth="100" HorizontalContentAlignment="Right"/>
    /// 
    ///     is giving 
    ///             Error 29    "The member "CurrentCulture" is not recognized or is not accessible."
    /// 
    /// Instead we use
    /// 
    ///         <TextBox Grid.Row="2" Grid.Column="1" 
    ///              Text="{CultureAwareBinding Path=SelectedSupporterCategory.Price, Mode=TwoWay, StringFormat='C',}" 
    ///              UseLayoutRounding="True" MaxWidth="100" HorizontalAlignment="Left" MinWidth="100" HorizontalContentAlignment="Right"/>
    /// 
    /// </summary>
    public class CultureAwareBinding : Binding
    {
        public CultureAwareBinding()
        {
            ConverterCulture = CultureInfo.CurrentCulture;
        }
    }
}
Geoff Scott
  • 897
  • 1
  • 10
  • 17
5

Changing the project target framework to .NET Framework 4.6 or higher solves the issue.

Go to solution explorer and right-click on the affected Project -> Properties -> Application -> Target framework.

kmb
  • 436
  • 1
  • 6
  • 13
3

found similar suggestion in this topic: WPF StringFormat={0:C} showing as dollars

my application was working when I launched it and diplayed values with correct culture formatting, but designer could not find CultureInfo.CurrentUICulture and crashed

I used static property in helper class

public static class WpfHelpers
{
    public static CultureInfo CurrentCulture { get; set; }
}

and used it in bindings: ConverterCulture={x:Static helpers:WpfHelpers.CurrentCulture}

I set that property on Application startup

WpfHelpers.CurrentCulture =
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture = new CultureInfo ...
Community
  • 1
  • 1
ASh
  • 34,632
  • 9
  • 60
  • 82