Automatically Get Complete Date dd/mm/YYYY from ddMM - wpf

Our Date Format is DD/MM/YYYY. If user puts the date and month only, then the app should pick up current year automatically. For Example: If a user puts 0104, then the app should get 01/04/2020. It needs to get the year automatically according to date
If the users selects Date on DatePicker ddmm(ex.0401) then the should automatically get Date in dd/mm/yyyy format(ex.04/01/2020)
<DatePicker
Grid.Column="3"
x:Name="FinancialYear"
HorizontalAlignment="Left"
Margin="74,317,0,0"
VerticalAlignment="Top"
SelectedDate="{Binding FinancialYear,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,StringFormat='dd/MM/yyyy',ValidatesOnDataErrors=False}"
Uid="none" Width="151"
PreviewKeyDown="FinancialYear_PreviewKeyDown">
</DatePicker>

Try to set the StringFormat of the binding to the DatePickerTextBox's Text property to whatever format you want. This works just fine for me:
<DatePicker x:Name="FinancialYear">
<DatePicker.Resources>
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd MM',
RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>

Related

Change telerik:RadTimeBar Intervals Label Color

I'm working with a telerik:RadTimeBar and as the title suggests I'm trying to change the color of the Interval's Label Color. The following is my RadTimeBar:
<telerik:RadTimeBar x:Name="PoCTimeBar" Width="450"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Background="#FF435061"
BorderBrush="{x:Null}"
BorderThickness="1,0,1,1"
Content=""
Cursor="Hand"
EnableSparklineVirtualization="False"
PeriodEnd="{Binding Tab.CurrentPoC.EndDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PeriodStart="{Binding Tab.CurrentPoC.StartDate}"
ScrollMode="None"
SelectionEnd="{Binding Tab.CurrentPoC.PickEnd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionStart="{Binding Tab.CurrentPoC.PickStart, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionTitleFormatString="{}{0:dd/MM/yy-HH:mm} - {1:dd/MM/yy-HH:mm}"
VisiblePeriodEnd="{Binding Tab.CurrentPoC.EndDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VisiblePeriodStart="{Binding Tab.CurrentPoC.StartDate}">
<telerik:RadTimeBar.Intervals>
<telerik:HourInterval />
<telerik:DayInterval />
<telerik:MonthInterval />
<telerik:YearInterval />
</telerik:RadTimeBar.Intervals>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<Custom:EventToCommand Command="{Binding Tab.IRefreshSpanPoC, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</telerik:RadTimeBar>
Could you please help me figure out how to achieve this? Thanks a lot.
You have two intervals' header in the RadTimeBar control, the top one and the one under it. For the top one you can change its color by setting the Foreground of the parent telerik:ItemControl, for the bottom one you target the telerik:GroupControl:
<Grid>
<telerik:RadTimeBar PeriodStart="1-Jan-2019 12:00:00 AM"
PeriodEnd="31-Dec-2019 11:59:59 PM"
VisiblePeriodStart="14-March-2019 12:00:00 AM"
VisiblePeriodEnd="11-Nov-2019 11:59:59 PM" >
<telerik:RadTimeBar.Resources>
<Style TargetType="telerik:ItemControl">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style TargetType="telerik:GroupControl">
<Setter Property="Foreground" Value="Blue" />
</Style>
</telerik:RadTimeBar.Resources>
<telerik:RadTimeBar.Intervals>
<telerik:YearInterval />
<telerik:MonthInterval />
<telerik:WeekInterval />
<telerik:DayInterval />
</telerik:RadTimeBar.Intervals>
</telerik:RadTimeBar>
</Grid>
How do i know this?
You have two tools to use in these scenarios in addition to the documentation:
Blend: you select the control> right click and then Edit template > Edit a copy.
Snoop
EDIT
Here the style applied to your code (with some minor adjustment to the start and end dates):
<telerik:RadTimeBar x:Name="PoCTimeBar" Width="450"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Background="#FF435061"
BorderBrush="{x:Null}"
BorderThickness="1,0,1,1"
Content=""
Cursor="Hand"
EnableSparklineVirtualization="False"
PeriodEnd="31-Dec-2019 11:59:59 PM"
PeriodStart="1-Jan-2019 12:00:00 AM"
ScrollMode="None"
SelectionEnd="{Binding Tab.CurrentPoC.PickEnd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionStart="{Binding Tab.CurrentPoC.PickStart, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionTitleFormatString="{}{0:dd/MM/yy-HH:mm} - {1:dd/MM/yy-HH:mm}"
VisiblePeriodEnd="{Binding Tab.CurrentPoC.EndDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VisiblePeriodStart="{Binding Tab.CurrentPoC.StartDate}">
<telerik:RadTimeBar.Resources>
<Style TargetType="telerik:ItemControl">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style TargetType="telerik:GroupControl">
<Setter Property="Foreground" Value="Blue" />
</Style>
</telerik:RadTimeBar.Resources>
<telerik:RadTimeBar.Intervals>
<telerik:HourInterval />
<telerik:DayInterval />
<telerik:MonthInterval />
<telerik:YearInterval />
</telerik:RadTimeBar.Intervals>
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<Custom:EventToCommand Command="{Binding Tab.IRefreshSpanPoC, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>-->
</telerik:RadTimeBar>
Output
Here without the styles:
Make sure that you aren't applying any other style somewhere else.

DatePicker do not show full month name at top because of style inheritance

I have a situation where i have to display the DatePicker , The problem is i am not able to see the complete month name at top, i am just able to see the first digit of december
with dots after like "D.." whereas there is enough space to show entire month name. Its because it is inheriting many style of textbox/textblock from controltemplate which i am not including.
Here is my try :
<DatePicker
FontSize="6"
SelectedDate="{x:Static sys:DateTime.Now}"
HorizontalContentAlignment="Left">
</DatePicker>
<Style TargetType="{x:Type DatePickerTextBox}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox
x:Name="PART_TextBox"
HorizontalAlignment="Left"
VerticalContentAlignment="Center"
Text="{Binding Path=SelectedDate, ConverterCulture={x:Static glob:CultureInfo.InvariantCulture}, StringFormat={}{0:yyyy-MM-dd}, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsReadOnly" Value="True" />
</Style>
<Style TargetType="{x:Type DatePicker}">
<Setter Property="Background" Value="{StaticResource FlowWhiteBrush}" />
</Style>
How to fix it such that we override the previous style by rewriting in the template written above and make it's alignment in left so that all month appears? But which styles and properties to be overridden?

WPF DatePicker - autocorrect options

I have a WPF DatePicker object.
<DatePicker Name="dtp_FilterByDate" />
and
<Application.Resources>
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Text" Value="Filter by date..." />
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox"
Text="{Binding Path=SelectedDate, StringFormat = {}{0:yyyy-MM-dd}, FallbackValue='1900-01-01', TargetNullValue='Enter a date...',
RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
When I manually enter a date into the box it automatically corrects it (I understand that it uses Date.Parse then formats it as I specify above), but it's ignoring the fact that my PC's culture is en-GB.
For example, If I enter
07/10/1988
it autocorrects it to
1988-07-10
i.e setting the month to 7 and the day to 10 - which is not expected behaviour for my PC's "en-GB" culture - which expects 1988-10-07 for the yyyy-MM-dd format specified.
If I enter
23/05/2007
The DatePicker shows an error, rather than parsing correctly.
Is there a way to set the culture for the datepicker (or generally for all Date.Parse calls in the application) manually, either to match my PC's or even to specify en-GB in all cases?
Edit - more information
DirectCast(dtp_FilterByDate.Template.FindName("PART_TextBox", dtp_FilterByDate), TextBox)
shows me that the Language is "en-US" - but I don't know why this is the case.
OK, just realised that I can update my style to do this.
Do the following:
<TextBox x:Name="PART_TextBox" Text="{...}" Language="en-GB"/>

Displaying an alternative text when no image has been assigned yet

I have an image control on which the user can drag and drop pictures. When no image has been dropped in there and the control is empty, I'd like to show an alternative text such as "Drop a picture here" to better indicate what is expected from him/her.
I can't figure out how to use triggers with this, this shows nothing at all and I can no longer drop pictures on my image control:
<StackPanel Grid.Column="1" Grid.Row="2" FlowDirection="LeftToRight" Orientation="Horizontal" Margin="0px 4px">
<StackPanel.Resources>
<DataTemplate x:Key="tmpTemplate">
<Border BorderThickness="2" BorderBrush="#FF969DFF" CornerRadius="2" VerticalAlignment="Top">
<DockPanel>
<Image Name="imgSelectedViewImage" Source="{Binding Image}" MinWidth="32" MinHeight="32" MaxWidth="48" MaxHeight="48"
HorizontalAlignment="Left" Stretch="None"
IsEnabled="{Binding EditMode}" Margin="2px"/>
<Label Content="Drag here" Name="AltText" Visibility="Collapsed"></Label>
</DockPanel>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Image}" Value="{x:Null}">
<Setter TargetName="AltText" Property="Visibility" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</StackPanel.Resources>
I can't tell you what you've done to break your previously working drag and drop functionality and all I can do is to suggest you undo your changes to remedy that. However, if you want to display something when a collection is empty, then there's an easy way to do that. Using the collection's Count property is no good because it won't update when items are added or removed.
You can simply add an int property next to your item collection for this, but you have to make sure that you notify the INotifyPropertyChanged interface for it also when the collection property changes:
public ObservableCollection<YourItem> YourItems
{
get { return yourItems; }
set
{
yourItems = value;
NotifyPropertyChanged("YourItems");
NotifyPropertyChanged("YourItemsCount");
}
}
public int YourItemsCount
{
get { return YourItems.Count; }
}
Then you can use it in a simple DataTrigger like this:
<Grid>
<!-- Put your normal content here -->
<TextBlock FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"
Text="Drop images here">
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding YourItemsCount}" Value="0">
<Setter Property="TextBlock.Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
I did it by setting the TargetNullValue and the FallbackValue to a static resource. See the code snippet below. You can of course set it to everything else you want.
<Image Stretch="Uniform"
Grid.Row="0"
Grid.Column="1"
MaxHeight="250"
MaxWidth="250"
Margin="10"
Source="{Binding Path=Character.Portrait, TargetNullValue={StaticResource FallbackImage}, FallbackValue={StaticResource FallbackImage}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<cal:ActionMessage MethodName="ChangePicture">
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</image>
Another approach, which could be more to your liking, as it uses triggers could be this solution: Handling null when binding to an Image in XAML (look at the accepted answer). However in my opinion setting TargetNullValue and the FallbackValue is the best way to go.

WPF TextBox setting Text using Trigger during validation

I am having a requirement where in I have to revert the values of a TextBox to old value when the user enters a wrong input. I am using MVVM framework so I dont want to write any codebehind. The Text and Tag of TextBox is databound from ViewModel variable. So my Tag field of TextBox will always have old value. I want to use the Tag field value to revert my Text value.
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right"
Foreground="Orange"
FontSize="12pt">
</TextBlock>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true" >
<Setter Property="ToolTip"
Value="{Binding Path=Tag,RelativeSource={RelativeSource Self}}">
</Setter>
<Setter Property="Text"
Value="{Binding Path=Tag,RelativeSource={RelativeSource Self}}">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
<TextBox Width="68" Tag="{Binding SampleText}" Height="23" HorizontalAlignment="Left" Margin="39,37,0,0" VerticalAlignment="Top" >
<TextBox.Text>
<Binding Path="SampleText" NotifyOnValidationError="True" ValidatesOnDataErrors="True" ValidatesOnExceptions="True">
<Binding.ValidationRules>
<val:SampleTextValidator></val:SampleTextValidator>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Now When an error happens, the TextBox is highlighted red.I have written a Trigger to revert the value back to original value (value stored in Tag field). Tt is not working. But Tooltip part is working. I am confused fully. Please help where am I doing wrong!!!. Correct me with a sample code if possible!!!!
My first guess is that when you made your text input invalid (eg. delete all values), you cause the tag to bind to the same value, hence, it will reflect an empty string.
What you need is a separate property for your original value to bind your tag to.
private string _oldValue;
public string OldValue
{
get {...}
set {... NotifyPropertyChanged()...}
}
private string _sampleText;
public string SampleText
{
get { return _sampleText; }
set {
OldValue = _sampleText;
_sampleText = value;
NotifyPropertyChanged(...);
}
}
<TextBox Width="68" Tag="{Binding OldValue}" ... >
Don't forget to implement INotifyPropertyChanged.

Resources