I'd like to use a custom DateTimeFormat across my entire Silverlight app, so I plugged in the following code in my App.xaml.cs.
var ci = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
ci.DateTimeFormat = new DateTimeFormatInfo()
{
ShortDatePattern = "dd/MM/yyyy",
LongDatePattern = "dd/MM/yyyy",
FullDateTimePattern = "dd/MM/yyyy HHmm\\h",
AMDesignator = string.Empty,
PMDesignator = string.Empty,
ShortTimePattern = "HHmm\\h",
LongTimePattern = "HHmm\\h"
};
Thread.CurrentThread.CurrentCulture = ci;
A DateTime.Now.ToString("f") gives me the answer I want (which is a 24-hour format DateTime string). However, when I do the following in XAML, the TextBlock ignores my CultureInfo and does its own thing:
<TextBlock Text="{Binding ArrivalTime,StringFormat=f}"/>
Does anybody know a way in which I can get the TextBlock to display in the format I desire? Many thanks...
Related
Similar questions were asked many times, but I cannot quite make them work. How would I create C# version of the following xalm statement:
<h:TubeVisual3D x:Name="PipeVisual" Path="{Binding Pipe.Path}"
TextureCoordinates="{Binding Pipe.TextureCoordinates}"
Diameter="{Binding ElementName=PipeDiamSlider, Path= Value }"
Material="{Binding Pipe.Material}"
BackMaterial="{Binding Pipe.Material}"
ThetaDiv="50" IsPathClosed="False"
Visible="{Binding ElementName=PipeIsVisibleCheck, Path=IsChecked}"/>
Where “TubeVisual3D” is a 3D WPF element defined in Helix Toolkit and most of the parameters bound by the binding are dependency properties in “TubeVisual3D”.
The equivalent to your markup would be this:
HelixToolkit.Wpf.TubeVisual3D pipeVisual = new HelixToolkit.Wpf.TubeVisual3D();
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.ExtrudedVisual3D.PathProperty, new Binding("Pipe.Path"));
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.ExtrudedVisual3D.TextureCoordinatesProperty, new Binding("Pipe.TextureCoordinates"));
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.TubeVisual3D.DiameterProperty, new Binding("Value") { Source = PipeDiamSlider });
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.MeshElement3D.MaterialProperty, new Binding("Pipe.Material"));
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.MeshElement3D.BackMaterialProperty, new Binding("Pipe.Material"));
pipeVisual.ThetaDiv = 50;
pipeVisual.IsPathClosed = false;
BindingOperations.SetBinding(pipeVisual, HelixToolkit.Wpf.MeshElement3D.VisibleProperty, new Binding("IsChecked") { Source = PipeIsVisibleCheck });
I am adding the columns to a DataGrid through the following code:
dataGrid.Columns.Add(new DataGridTextColumn { Header = "n", Binding = new Binding("n") { StringFormat = "{0:n2}" } });
The data I am binding to is a DataTable retrieved from a database. The value in the DataTable is of type money in the database, thus when displayed in the DataGrid the values would be something like 1000.0000.
What is the StringFormat set to in order to remove the decimal places because StringFormat = "{0:n2}" does not work.
Use this :
Binding = new Binding("n") { StringFormat = "{0,1:F4}" } });
In a small Wpf application, use a DatePicker bound to a DateTime property.
When the user's region and language settings, and number and date format are German, the date is displayed in German, and the calendar shows German month names.
Now I wanted to get it in US-English. In the c'tor of MainWindow I added before InitializeComponent() (same situation when doing that after InitializeComponent()):
string uiLanguage = ConfigurationManager.AppSettings["UILanguage"]; //"en-US"
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(uiLanguage);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(uiLanguage)));
While that works with textboxes, it has no effect with the DatePicker.
Then I was cheaky and created a new user "John English", logged in as "John English", set his display language to English and the date and number format to US-English. Now the DatePicker always displays the date in the US-English format and the calendar shows English month names, even when I set the language of my program to German.
How can that be resolved? Can it be resolved at all?
In the codebehind of the App.xaml add the following code:
public App()
{
EventManager.RegisterClassHandler(typeof(DatePicker), DatePicker.LoadedEvent, new RoutedEventHandler(DatePicker_Loaded));
}
void DatePicker_Loaded(object sender, RoutedEventArgs e)
{
var dp = sender as DatePicker;
if (dp == null) return;
var tb = GetChildOfType<DatePickerTextBox>(dp);
if (tb == null) return;
var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
if (wm == null) return;
wm.Content = "[Put your text here]";
}
[Ontopic ;)]
Try setting both CurrentCulture and CurrentUICulture.
//Set default culture to Nl
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
ref for GetChildOfType
Create a normal DataGridTextColumn without any bindings in the XAML:
<DataGridTextColumn x:Name="SomeDateDataGridColumn" Width="Auto" Header="Header"/>
Then set the binding and stringformat in the code behind:
SomeDateDataGridColumn.Binding = new Binding("Property") { StringFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern };
I have bound a my chart to a result of load operation. The result returned by the load operation returns complex type with values for some of the object's properties is null.
So I am getting an error in my silver light application.
The code of my XAML is as follows:
<Grid x:Name="LayoutRoot">
<toolkit:Chart Name="historicalTotalChart" Title="Chart Title">
<toolkit:LineSeries >
</toolkit:LineSeries>
</toolkit:Chart>
</Grid>
Code of in my CS file is as follows:
LoadOperation<GetHistoricalTotalReport_Result> hisTotalsLoadOp =
context.Load(context.GetHistoricalToalReportQuery(tableName, sD, startDate, endDate));
LineSeries line = new LineSeries();
line.ItemsSource = hisTotalsLoadOp.Entities;
//line.DependentValuePath ="rep_date";
//line.IndependentValuePath = "expr1";
line.DependentValueBinding = new Binding("[rep_date]");
line.IndependentValueBinding = new Binding("[expr1]");
line.Title = "Historical Totals";
historicalTotalChart.Series.Add(line);
Can any one say how can I over come this error?
rep_date, expr1 are the properties with in my complex type GetHistoricalTotalReport_Result. I am I binding to the properties correctly?
Any help much appreciated.
Thanks
There are 3 possible issues in your application:
Your bindings shouldn't contain square brackets, they are for arrays and dictionaries. Also I would rather use the properties Dependent/IndependentValuePath, you shouldn't comment them, they are completely correct.
The DependentValuePath property must be of a numerical data type, because it is situated on the Y-axis. In your example the rep_date property could be of the double type.
The IndependentValuePath is situated on the X-axis and could be of any type but the values can't contain nulls. Really, I have no idea where a null value can be displayed on the X-axis, it doesn't make sense.
Here is the example of the correct code which works fine:
LineSeries line = new LineSeries();
line.ItemsSource = new[]
{
new ItemViewModel{expr1 = "Item1", rep_date = 25},
new ItemViewModel{expr1 = "Item2", rep_date = null},
new ItemViewModel{expr1 = "Item3", rep_date = 31},
new ItemViewModel{expr1 = "Item4", rep_date = null},
};
line.DependentValuePath = "rep_date";
line.IndependentValuePath = "expr1";
line.Title = "Historical Totals";
historicalTotalChart.Series.Add(line);
The next code will not work:
line.ItemsSource = new[]
{
new ItemViewModel{expr1 = null, rep_date = 25} //wrong, x-value can't be null
}
But you can filter your entities before displaying them:
line.ItemsSource = hisTotalsLoadOp.Entities.Where(e => e.expr1 != null).ToList();
I cannot get the axis to format as currency, any idea?
What am I doing wrong? I need to be able to change the formatting on the fly and for this test I wanted to set it as currency for the Y axis on the scale of values.
Anyone?
Thanks...
var columnSeries = new ColumnSeries
{ Title = reportProcedureNode.Value,
IndependentValuePath = "PrimaryKey",
DependentValuePath = "Value",
IndependentAxis = new CategoryAxis { Orientation = AxisOrientation.X, ShowGridLines = false, Location = AxisLocation.Bottom},
DependentRangeAxis = new LinearAxis(){Orientation = AxisOrientation.Y, ShowGridLines = false}
};
var labelStyle = new Style(typeof(AxisLabel));
labelStyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "{}{0:C0}"));
var axis = (LinearAxis)columnSeries.DependentRangeAxis;
axis.AxisLabelStyle = labelStyle;
In my WPF4 version of the charting toolkit, your code crashes. I needed to change:
labelStyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "{}{0:C0}"));
to:
labelStyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "{0:C0}"));
That is, remove the {}. The {} comes from markup extension syntax:
{} Escape Sequence / Markup Extension
and is only needed when being parsed by XAML as a markup extension inside "{...}".
Since you are setting the property directly, no markup extension is involved and including it prevents the real currency format from being seen.