Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
C#
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
HTML
<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
Posted
Updated 20-Apr-15 23:39pm
v2
Comments
Herman<T>.Instance 21-Apr-15 5:41am    
Better show the code that implements the country value from the above list.
Member 11428367 21-Apr-15 6:27am    
ode ???
Herman<T>.Instance 21-Apr-15 6:45am    
sorry... typo... code
Member 11428367 21-Apr-15 8:15am    
Code in view model
class MainViewModel : INotifyPropertyChanged
{

private string _patientName;
private string _sex;
private string _countryName;
private string _doctorName;
private int? _age = null;
private string _appointmentDate = null;
private int? _billAmount = null;
public DataTable _table;

public List<string> _countrylist;
List<string> cultureList = new List<string>();
List<string> abbrevationtext = new List<string>();


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.LCID);

if (!(cultureList.Contains(region.EnglishName)))
{
cultureList.Add(region.EnglishName);
//abbrevationtext.Add("en-" + region.TwoLetterISORegionName.ToLower());
string abbrevation = region.TwoLetterISORegionName;
string abbrevationtext = "en-" + abbrevation.ToUpper();

}
}

catch (Exception ex)
{
throw ex;
}

}
cultureList.Sort();

return cultureList;


}

public List<string> _listofdoctors = new List<string> { "Rama Sankar", "Manjula", "Srinivasa Sarma", "Ramesh Samy", "Manoj Kumar" };
public List<string> ListOfDoctors
{
get { return _listofdoctors; }
}



public List<string> CountryList
{
get { return _countrylist; }
set { _countrylist = value; }
}

DataTable dt = new DataTable();
public DataTable Table
{
get { return _table; }
set
{
_table = value;


if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Table"));
}
}

public string PatientName
{
get { return _patientName; }
set { _patientName = value; }
}


public string Sex
{
get { return _sex; }
set { _sex = value; }
}

public string CountryName
{
get { return _countryName; }
set
{
if (_countryName != value)
{
_countryName = value;

}
}
}

public string DoctorName
{
get { return _doctorName; }
set { _doctorName = value; }
}

public int? Age
{
get { return _age; }
set { _age = value; }
}


public string AppointmentDate
{
get { return _appointmentDate; }
set { _appointmentDate = value; }
}

public int? BillAmount
{
get { return _billAmount; }
set { _billAmount = value; }
}

public RelayCommand Add { get; set; }
public RelayCommand SetCulture { get; set; }


public MainViewModel()
{
Add = new RelayCommand(Adding);
_countrylist = getCountryList();
SetCulture = new RelayCommand(SettingCulture);
}
public void Adding()
{
if (dt.Rows.Count == 0)
{
dt.Columns.Add("PatientName");
dt.Columns.Add("Age");
Member 11428367 21-Apr-15 8:16am    
dt.Columns.Add("CountryName");
dt.Columns.Add("DoctorName");
dt.Columns.Add("AppointmentDate");
dt.Columns.Add("BillAmount");
}
dt.Rows.Add(PatientName, Convert.ToString(Age), Sex, CountryName, DoctorName, Convert.ToString(AppointmentDate), Convert.ToString(BillAmount));
Table = dt;

}

public void SettingCulture()
{
try
{

CultureInfo culture = new CultureInfo("abbreviation", true);

System.Threading.Thread.CurrentThread.CurrentCulture = culture;

DateTime dt = DateTime.Now;
}
catch(Exception ex)
{
throw ex;
}

}



public event PropertyChangedEventHandler PropertyChanged;



1 solution

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:

XML
<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:
C#
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.
 
Share this answer
 
Comments
Member 11428367 23-Apr-15 2:45am    
hi,
I am unable to get what you are telling.Can i have clear code and description about that please.
Thank You.
Jason Gleim 23-Apr-15 10:39am    
Do you understand how in-line styles (or styles in general) work in xaml or how value converters are used? With an understanding of either of these concepts you should be able to create a functional solution. The fact is, I can't write the code for you... I don't have enough information about your requirements and I've got my own work to do.

I've suggested to you two different options; one that would work while remaining on the path you have started down. Another which changes the control you would use but puts more control over the formatting into your hands via a value converter and a property on your view model. Although I suggest the second, you have to make the decision on which path you will choose.

Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900