The member “CurrentCulture” is not recognized or is not accessible

前端 未结 3 1150
轮回少年
轮回少年 2021-01-07 21:50

I have a Window with the following namespace

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

that contains a textbox

相关标签:
3条回答
  • 2021-01-07 22:29

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-07 22:44

    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.

    0 讨论(0)
  • 2021-01-07 22:45

    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 ...
    
    0 讨论(0)
提交回复
热议问题