如何在wpf中从combobox中选择不同的文化时更改datepicker中的日期 [英] how to change date in datepicker when selecting different cultures from combobox in wpf

查看:114
本文介绍了如何在wpf中从combobox中选择不同的文化时更改datepicker中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在combobox中创建了一个国家/地区列表,我有datepicker。当用户选择一个国家时,日期必须根据该国家文化格式进行更改。请帮助我

  public  List< string> getCountryList()
{

CultureInfo [] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures& ~HeultureTypes.NeutralCultures& ~HeultureTypes.InstalledWin32Cultures);
foreach (CultureInfo culture 文化中)
{

尝试
{
RegionInfo region = new RegionInfo(culture.Name) ;

if (!(cultureList.Contains(region.EnglishName)))
{
cultureList.Add(region 。英文名);
abbrevationtext.Add( en - + region.TwoLetterISORegionName.ToLower());

}
}

catch (例外情况)
{
throw ex;
}

}
cultureList.Sort();

return cultureList;


}





我的xaml如下

 <  标签   内容  =  CountryName    Horizo​​ntalAlignment   =  Left    VerticalAlignment   =  Top     背景  = #FFBB4040   宽度  =  110   高度  =  25  /  >  

< combobox horizo​​ntalalignment = itemssource = {Binding CountryList} selecteditem = {Binding CountryName,Mode = TwoWay} < span class =code-attribute> verticalalignment = 顶部 width = 120 height = 25 iseditable = False / >

< 标签 内容 = AppointmentDate Horizo​​nta lAlignment = VerticalAlignment = Top 背景 = #FFBB4040 宽度 = 120 < span class =code-attribute>高度 = 25 / >

< datepicker x :name = dtpAppointment Horizo​​ntalAlignment = left = Text = verticalalignment = Top height = 25 xmlns: x = #unknown / >







我有添加按钮,这里使用MVVM模式

解决方案

您可以通过内嵌或在页面资源或app.xaml中的资源中指定默认样式来覆盖datepicker控件的样式。样式可能类似于:



 <  样式    targettype   =  {x:输入DatePickerTextBox} >  
< setter 属性 = Control.Template >
< setter.value >
< controltemplate >
< < span class =code-leadattribute> textbox x:name = PART_TextBox xmlns:x = #unknown >
Text ={Binding Path = SelectedDate,
Converter = {StaticResource localizationConverter}},
ConverterParameter = {Binding CountryName},
RelativeSource = { RelativeSource AncestorType = {x:Type DatePicker}}}/>
< / textbox > < / controltemplate >
< / setter.value >
< / setter >
< / style >





您也可以将国家/地区名称指定为动态资源,并在飞。这里的关键是您将国家/地区名称转换为可以为您更改文本表示的值转换器。转换器应该相对简单。转换方法的主体应该是:

  return  字符串 .Format(culture,  {0:D}); 





您将文化转换为正确的文化对象转换器参数。



或者,我认为更好,就是切换到WPF Toolkit中的DateTimePicker控件。它有一个Format属性,你设置为'Custom',然后是一个FormatString属性,你可以绑定到视图模型上的东西。当用户从组合框中选择国家/地区时,在CountryName的属性设置中,为选择器控件设置适当的格式字符串。应该那么干净。


hi,
I have created a country list in combobox and i have datepicker.when an user select a country the date have to change withrespect to that country culture format.please help me

public List<string> getCountryList()
        {
           
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures & ~CultureTypes.InstalledWin32Cultures);
            foreach (CultureInfo culture in cultures)
            {

                try
                {
                    RegionInfo region = new RegionInfo(culture.Name);

                    if (!(cultureList.Contains(region.EnglishName)))
                    {
                        cultureList.Add(region.EnglishName);
                        abbrevationtext.Add("en-" + region.TwoLetterISORegionName.ToLower());
                    
                    }
                }
                    
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            cultureList.Sort();
            
            return cultureList;
            

        }  



and i have xaml as below

<Label Content="CountryName" HorizontalAlignment="Left" VerticalAlignment="Top"  Background="#FFBB4040" Width="110" Height="25"/>
        
<combobox horizontalalignment="Left" itemssource="{Binding CountryList}" selecteditem="{Binding CountryName,Mode=TwoWay}" verticalalignment="Top" width="120" height="25" iseditable="False" /> 
           
<Label Content="AppointmentDate" HorizontalAlignment="Left" VerticalAlignment="Top"  Background="#FFBB4040" Width="120" Height="25"/>

<datepicker x:name="dtpAppointment HorizontalAlignment=" left=" Text=" verticalalignment="Top" height="25" xmlns:x="#unknown" />




and i have Add button and here im using MVVM pattern

解决方案

You can override the style of the datepicker control by either in-lining it or specifying a default style in the page resources or in the resources in app.xaml. The style might look something like:

<style targettype="{x:Type DatePickerTextBox}">
 <setter property="Control.Template">
  <setter.value>
   <controltemplate>
    <textbox x:name="PART_TextBox" xmlns:x="#unknown">
     Text="{Binding Path=SelectedDate,
     Converter={StaticResource localizationConverter}},
     ConverterParameter={Binding CountryName},
     RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
   </textbox></controltemplate>
  </setter.value>
 </setter>
</style>



You might also be able to specify the country name as a dynamic resource and change it on the fly. The key here is that you get the country name into a value converter that can change the text representation for you. The converter should be relatively straightforward. The body of the convert method should just be:

return String.Format(culture, "{0:D}", value);



where you have cast culture to the correct culture object as passed via the converter parameter.

Alternately, and I think better, would be to switch to the DateTimePicker control that is in the WPF Toolkit. It has a Format property you set to 'Custom' then a FormatString property you can bind to something on your view model. When the user selects a country from the combobox, in the property setting for CountryName, set the appropriate format string for the picker control. Should be much cleaner that way.


这篇关于如何在wpf中从combobox中选择不同的文化时更改datepicker中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆