Setting default value of DateTimePicker - c#

Currently my DateTimePickers are blank, I would like them to have a default setting i.e
Today.AddYears(20); & Today.AddDays(5);
What I have done at the moment is 'DateTimePicker.Text = DateTime.Today.AddYears(-100).ToString();' As you can probably tell, this isn't working hence why I'm asking the question.
Bit of information :- I need it so that if they arent changed and the button is pressed it uses the default dates. If they are changed, I obviously need to use the changed dates.
Suggestions.
XAML ADDED:
<igEditors:XamDateTimeEditor Name="startDateTimePicker"
Grid.Row="1"
Grid.Column="1"
xmlns:igEditors="http://infragistics.com/Editors"
Height="18"
Width="100"
Format="yyyy-MM-dd"
VerticalAlignment="Top"
HorizontalAlignment="Center" />

Setting the Value property should do the trick:
DateTimePicker.Value = DateTime.Today.AddYears(-100);
And since it's type is DateTime, you don't need any ToString conversions.

Just set the value property of DateTimePicker Control.

Related

C# WPF DatePicker value when opened picker

I would like to ask you about any trick with DatePicker. I have default value DateTime.MinValue, but I want, when I open the DatePicker, to show actual Date, so I dont have to click milion times to find actual date.
Is there any solution?
<DatePicker SelectedDate="{Binding RecievedDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Thank you very much.
It looks like what you are doing is correct so for some reason the binding must be failing (check output window) or you need to update the binding via INotifyPropertyChanged

How to set and get tag in xaml dynamically?

While creating GUI using xaml, I created a textbox with a tag like this:
<TextBox Name="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="216,178,143,120" Width="158"
Tag="myTag"/>
Now I want to let the user be able to change this tag. For that, I am looking for a kind of function of form:
TextBox.SetTag( "User Provided Tag" )
So that the tag can be changed into this one:
<TextBox Name="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="216,178,143,120" Width="158"
Tag="User Provided Tag"/>
After searching the internet for quite a while, I didn't come up with any practical solution. Could someone help? Thanks.
You can use a binding between two controls. Say a user is allowed to enter a tag value from a TextBox. You just bind the Tag of the second TextBox to a Text property of the first TextBox:
<TextBox Name="enterTagTextBox" />
<TextBox Name="getTagTextBox" Tag="{Binding ElementName=enterTagTextBox, Path=Text}"/>
To test it I added a Button in my XAML:
<Button Height="25" Click="Button_Click_1"/>
In code behind I just retrieve the tag value and display it like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string text = this.getTagTextBox.Tag.ToString();
global::System.Windows.Forms.MessageBox.Show(text);
}
I think your problem may arise from you naming your TextBox, "TextBox". Try giving it a name that does not clash with the class name, like "txtMyTextBox".
Then you can do, txtMyTextBox.Tag = "User Provided Tag";.
Or you can bind to it as PiotrWolkowski suggests.
However, I would like to add, that it seems like there should be a cleaner way of achieving your desired behaviour. With the caveat that I don't know the details of what you are trying to implement.
I strongly suggest creating a ViewModel (see: MVVM Pattern) to hold the data you want users to be able to edit. Then use the bindings in WPF to display the data.
You would need to use the property element usage in order to set the Tag property in Extensible Application Markup Language (XAML) to anything other than an object with a known and built-in type converter, such as a string.

WPF formatting labels with StringFormat

I have a WPF application. I have some labels and some datagrids which are bound to some public properties. Some of these properties are numerical values.
In the datagrids I have been using the line below to ensure the values only display two decimal places, which works. However when I use the same line below for my label it appears to have no effect on the display as the number shows to about 9 decimal places. I don't understand why it works for the datagrid but not the label?
StringFormat={}{0:0.##}
<Label Grid.Row="3" Grid.Column="1"
Content="{Binding Obs.Tstat, StringFormat={}{0:0.#}}"
HorizontalAlignment="Center" Foreground="{StaticResource brushLinFont}"
FontSize="13" FontWeight="Bold"/>
Updated code
<Label Grid.Row="3" Grid.Column="1"
Content="{Binding Obs.Tstat}" ContentStringFormat="{}{0:0.#}}"
HorizontalAlignment="Center" Foreground="{StaticResource brushLinFont}"
FontSize="13" FontWeight="Bold"/>
For label you need to use ContentStringFormat:
<Label Content="{Binding Obs.Tstat}" ContentStringFormat="{}{0:0.##}"/>
Reason:
Label's Content property is of type object and StringFormat is used only when binding property is of type String.
If you try your code with TextBlock's Text property it will work fine with StringFormat because Text property is of type string.
Just a quick addition I'd like to post along these lines in case anyone else runs in to it... My application uses localization since it has multi-country support, but we also support user's system settings. We noticed ContentStringFormat defaults to your UI culture.
That caused an issue in one of our forms where the user's machine-specific decimal places they had configured in windows settings were not respected when you specified the ContentStringFormat.
Since the ContentPresenter simply takes the string format without the converter culture you could normally specify in the binding , this means that a format of say: 0:N will only return two decimal places if English is my current UI culture, even though I have 5 decimals specified on my windows settings.
In our case we applied some binding overrides to work around this, but mainly just wanted to add this extra bit of info in case anyone else runs in to it ;)

Set the minValue directly in a DateTimePicker

Is there a way to set a minValue directly in a DateTimePicker and not in a DateTime.
I use WPF and dotnet 4.0.
this might be your answer.
<DatePicker Grid.Row="2" DisplayDateStart="1/1/2013" DisplayDateEnd="5/10/2013" />
if not please provide more detaied question.

Binding data values in WPF

I am binding the data of a textbox through databinding.
Instead of giving the minimum and maximum value like this ...
<TextBox Height="24"
HorizontalAlignment="Right"
abc:TextBoxMaskBehaviour.Mask="Decimal"
abc:TextBoxMaskBehaviour.MinimumValue="0"
abc:TextBoxMaskBehaviour.MaximumValue="200"
Margin="0,9,8.5,0" Name="txtCStart"
VerticalAlignment="Top"
Width="106"
MouseWheel="OnMouseWheel">
I want to give it throught .xaml.cs file. How to do that? Help me please
Apparently (judging from your comments, your question is not very clear), you want to set the abc:TextBoxMaskBehaviour.MinimumValue and MaximumValue through C# code:
TextBoxMaskBehaviour.SetMinimumValue(txtCStart, 0);
TextBoxMaskBehaviour.SetMaximumValue(txtCStart, 200);
In general, you set attached properties like this: AttachedPropertyClass.SetAttachedProperty(Control, Value). Likewise, the value can be read with AttachedPropertyClass.GetAttachedProperty(Control).
This looks like it is probably directly related to the Rubenhak TextBoxMask features (and hence I found it looking to answer this exact question). For any who follow, in the context of Rubenhak, the answer is:
Rubenhak.Common.WPF.TextBoxMaskBehavior.SetMask(
TextBoxControl,
Rubenhak.Common.WPF.MaskType.Decimal);
And similarly for:
Rubenhak.Common.WPF.TextBoxMaskBehavior.SetMinimumValue()
Rubenhak.Common.WPF.TextBoxMaskBehavior.SetMaximumValue()

Categories

Resources