Silverlight tips (1)

Source: Internet
Author: User

By the end of the year, the tasks on hand have come to an end, and the company's product Web end has recently upgraded to sl4, finally, we can be totally different from the suffering of no visual designer during vs08 development.

Using the free time of the past few days, you can sort out the problems you encounter on the Internet, even by integrating your knowledge.

Datapicker watermark Processing

If you want to change the watermark content here, it may not be easy to implement. A watermark attribute is published for a part of the datepickertextbox control.

 
[Templatepart (name ="Textbox", Type =Typeof(Datepickertextbox)]
Public ClassDatepicker: Control

The easiest way to change the watermark is to inherit from datapicker and override onapplytemplate ().

 
Public ClassMydatepicker: datepicker {
 
Public Override VoidOnapplytemplate ()
{
 
Base. Onapplytemplate ();
 
Datepickertextbox =Base. Gettemplatechild ("Textbox")
 
AsDatepickertextbox;
Datepickertextbox. Watermark ="Select date...";
 
}}

Put this control in the page and you will see this effect

If you want to prompt the user input according to the datepicker format, you can simply change the precedingCode

 
Public Override VoidOnapplytemplate ()
{
 
Base. Onapplytemplate ();
 
Datepickertextbox =Base. Gettemplatechild ("Textbox")
 
AsDatepickertextbox;
StringFormatstring = string. empty;
 
If(This. Selecteddateformat = datepickerformat. Short)
 
Formatstring ="Mm/d/YYYY";
If(This. Selecteddateformat = datepickerformat. Long)
 
Formatstring ="Dddd, Mmmm, DD, yyyy";
 
Datepickertextbox. Watermark ="Enter :("+ Formatstring +")";
 
}

This is the case:

However, the above method can only ensure that this effect is displayed for the first time. In other words, when you enter a value and delete it, the watermark information will display the original value.

Fallbackvalue attribute

In sl4, bind adds multiple attributes. fallbackvalue is used to set the content displayed on the UI when the bound field does not exist.

 
<TextblockText="{Binding Path = Name, fallbackvalue = 'invalid value '}"/>

When the bound source does not have the name attribute, textblock displays "invalid value ".

Stringformat and currentculture

In sl3, if you want to change the format of the display string, you can only use ivalueconverter. Binding in sl4 provides the stringformat attribute to simplify this work:

 
<TextblockText="{Binding Path = data, stringformat = now is \ {0: G \}}"/>
<TextblockText="{Binding Path = data, stringformat = now is \ {0: yyyy-m-dd \}}" />
 
<TextblockText="{Binding Path = data, stringformat = 'now is {0: yyyy-m-dd }'}" />

The stringformat attribute sets the format of the bound object.

In this example, when multiple languages are involved, stringfomat cannot correctly display the corresponding cultural format. First, repeat the scenario:

Bind the current culture and time to the page:

 
<Textblock Text="{Binding Path = data, stringformat = now is: \ {0: G \}}"/>
<Textblock Text="{Binding Path = Name, stringformat = 'ubuntureinfo name is: {0 }'}" />

The interface displays the following results:

We can see that the current culture is ZH-CN. Now we can set another culture. Here it is a little simpler, without the use of language packs, you can only add the culture/uiculture parameter to <object/> On the ASPX page.

<Param Name="Culture" Value="ZH-tw" />

At this time, you will see the page shown as follows:

Obviously, although the system has identified different environments, stringformat does not display the corresponding time format.

Add the following code to the page constructor:

 
This. Language = xmllanguage. getlanguage (thread. currentthread. currentculture. Name );

The Code sets the current culture as the UI Language, and the page you see will be like this:

Obviously, stringformat can automatically change the format.

On the Internet, you can also see another simpler method. You can directly set XML: lang = "ZH-tw" in the Xmal root element. stringformat automatically adapts to the format, however, this method is not good,

After all, you will not use this method to set multilingual settings.

ComboBox selected items

ComboBox has an isselected attribute to set the selected items. However, when an item is added dynamically, setting isselected is invalid.

 
Comboboxitem;
 
For(IntI = 0; I <5; I ++)
{
 
Comboboxitem =NewComboboxitem ();
 
Comboboxitem. content ="Item"+ I;
 
This. Combobox1.items. Add (comboboxitem );
}
 
If(Combobox1.items. Count> 0)
 
{
 
Comboboxitem = combobox1.items [0]AsComboboxitem;
Comboboxitem. isselected =True;
 
}

You will find that the first item is not displayed on ComboBox, but it is very easy to solve this problem.

 
  If  (combobox1.items. count> 0) 
{
 combobox1.selectedindex = 0; 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.