接着上篇继续
1.1. Label
<sdk:Label Height="28" HorizontalAlignment="left" margin="122,46,0" name="label1"VerticalAlignment="top" WIDth="120" />
1.2. ListBox
前台代码:
<!--SelectionMode属性确定用户一次可以选择多少个项。可以将此属性设置为 Single(默认值)、Multiple或Extended。下表描述了这些枚举值的行为。
Single
用户一次只能选择一项。
Multiple
用户可以选择多个项而无需按下修改键。
Extended
用户可以按下 Shift键来选择多个连续项,或按下 Ctrl键并单击项来选择多个不连续的项。-->
<ListBox Height="278" ItemsSource="{Binding}" HorizontalAlignment="left" margin="116,130,0" name="ListBox1"VerticalAlignment="top" WIDth="328" SelectionChanged="ListBox1_SelectionChanged">
</ListBox>
后台代码:
public ListBoxP()
{
InitializeComponent();
List<string> lst =new List<string>();
for (int i = 0; i< 100; i++)
{
lst.Add("张三" + i.ToString());
}
this.ListBox1.ItemsSource = lst;
}
private voIDListBox1_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
MessageBox.Show(this.ListBox1.SelectedItem.ToString());
}
1.3. MediaElement
前台代码:
<MediaElement Source="Media/九阴真经.wmv" Autoplay="True" Height="244" HorizontalAlignment="left" margin="109,45,0" name="mediaElement1"VerticalAlignment="top" WIDth="420" />
后台代码:
// MediaElement.Play() -播放视频
// MediaElement.Pause() -暂停视频
// MediaElement.Stop() -停止视频
// MediaElement.IsMuted -是否静音
// MediaElement.Volume -声音大小(0 - 1)
1.4. MultiScaleImage
1.5. OpenfileDialog
后台代码:
OpenfileDialogo =new OpenfileDialog();
o.Multiselect = true;
o.Filter = "文本文件|*.txt|所有文件|*.*";
o.ShowDialog();
1.6. Page
1.7. PasswordBox
<PasswordBox Password="123"PasswordChar="*" Height="23"HorizontalAlignment="left" margin="140,157,0" name="passwordBox1"VerticalAlignment="top" WIDth="120" />
1.8. Popup
//HTMLPopupWindowOptions - 需要d出的新窗口的参数(如果浏览器是以标签的形式打开新窗口,则此参数无效)
HTMLPopupWindowOptions opt =new HTMLPopupWindowOptions();
opt.left = 0;
opt.top= 0;
opt.WIDth = 320;
opt.Height = 240;
// HTMLPage.IsPopupWindowAllowed -指定Silverlight 宿主是否可以使用 HTMLPage.PopupWindow()来d出新的浏览器窗口
// HTMLPage.PopupWindow() -d出新窗口
if (true ==HTMLPage.IsPopupWindowAllowed)
HTMLPage.PopupWindow(newUri("http://www.baIDu.com/",UriKind.absolute),"newWindow",opt);
1.9. Progressbar
前台代码:
<Progressbar Height="24" HorizontalAlignment="left" margin="134,225,0" name="progressbar1" VerticalAlignment="top" WIDth="314"/>
后台代码:
this.progressbar1.Maximum= 10000;
this.progressbar1.Minimum = 0;
for (int i = 0; i< 10000; i++)
{
this.progressbar1.Value += i;
}
1.10. Radiobutton
前台代码:
<Radiobutton Content="男" Height="16" IsChecked="True" HorizontalAlignment="left" margin="136,82,0" name="radiobutton1" VerticalAlignment="top" />
<Radiobutton Content="女" Height="16" HorizontalAlignment="left" margin="136,104,0" name="radiobutton2" VerticalAlignment="top" />
1.11. Repeatbutton
类似button
1.12. RichTextBox
前台代码:
<RichTextBox HorizontalAlignment="left" DataContext="123" margin="222,47,0" name="richTextBox1" VerticalAlignment="top" Height="114" WIDth="250" />
后台代码:
在Silverlight4版本中,RichTextBox这个控件算是很受期待的控件了,希望通过这篇文章让你对该控件有所了解。
在Sl中,有TextBox,TextBlock,RichTextBox这3个核心的文本控件,从表面上看,RichTextBox看起来和一个普通的TextBox并没有太多的区别,实际上它提供了诸如格式化文本,段落,超链接,内联图像这些功能,它甚至可以显示任何UIElement,比如DataGrID。
MSDN有关于RichTextBox内容模型的一个关系图
RichTextBox的内容属性是Blocks,这是一个Paragraph的集合,这些Paragraph可以包含Inline元素派生的元素。比如Run,
Span,Bold,Italic,Hyperlink,InlineUIContainer,其实从图中你可能会注意到Hyperlink比较特殊,它不能包含其它Inline类型的元素。
下面熟悉RichTextBox中一些简单的属性:
添加RichTextBox:
1. <RichTextBox AcceptsReturn="True"x:name="rbtMyRichTextBox">
<RichTextBoxAcceptsReturn="True" x:name="rbtMyRichTextBox">
RichTextBox常用的属性包括AcceptReturn和IsReadonly属性,只有在只读模式下,UI元素才是可用的。
添加元素:前面已经提到,RichTextBox包含了Paragraph集合,所以我们可以轻易添加任何内联元素,那么这里我们看一看在一个Paragraph中添加多个元素的Code
1. Paragraph prgParagraph =newParagraph();
2. Run rnMyText =new Run();
3. rnMyText.Text ="Thisis some example text with a ";
4. prgParagraph.Inlines.Add(rnMyText);
5. Hyperlink lnkSSlink =newHyperlink();
6. lnkSSlink.Inlines.Add("linkto Silverlight Show");
7. lnkSSlink.NavigateUri =new Uri("http://www.baIDu.com");
8. prgParagraph.Inlines.Add(lnkSSlink);
9. rbtMyRichTextBox.Blocks.Add(prgParagraph);
ParagraPHPrgParagraph = new Paragraph();
Run rnMyText = newRun();
rnMyText.Text ="This is some example text with a ";
prgParagraph.Inlines.Add(rnMyText);
Hyperlink lnkSSlink= new Hyperlink();
lnkSSlink.Inlines.Add("linkto Silverlight Show");
lnkSSlink.NavigateUri= new Uri("http://www.mrtcode.com");
prgParagraph.Inlines.Add(lnkSSlink);
rbtMyRichTextBox.Blocks.Add(prgParagraph);
上面的代码中,添加了一些文本和一个超链接,那么首先要做的是实例化一个Paragraph,在这个Paragraph中,我们添加了文本和超链接,这些对象被添加到该Paragraph中的Inlines集合中,最后将这个Paragraph添加到了RichTextBox中
格式文本对象:Run元素只能包含非格式化的文本,如果想要对文本进行格式化,那么可以采用变通的方式,即将Run元素包含在内联的格式元素中
1. Paragraph prgParagraph =newParagraph();
2. Bold bldText =new Bold();
3. bldText.Inlines.Add(new Run() {Text = "This is some example text in bold" });
4. Italic itlText =new Italic();
5. itlText.Inlines.Add(new Run() {Text = "This is some example text in italics" });
6. Underline unText =newUnderline();
7. unText.Inlines.Add(new Run() {Text = "This is some example text,underlined" });
8. Bold bldTextWithItalic =new Bold();
9. bldTextWithItalic.Inlines.Add(new Italic(){ Inlines = { new Run(){Text ="This is some example text,bold and italic" } } });
10. prgParagraph.Inlines.Add(bldText);
11. prgParagraph.Inlines.Add(newlineBreak());
12. prgParagraph.Inlines.Add(itlText);
13. prgParagraph.Inlines.Add(newlineBreak());
14. prgParagraph.Inlines.Add(unText);
15. prgParagraph.Inlines.Add(newlineBreak());
16. prgParagraph.Inlines.Add(bldTextWithItalic);
17. rbtMyRichTextBox.Blocks.Add(prgParagraph);
ParagraPHPrgParagraph = new Paragraph();
Bold bldText = newBold();
bldText.Inlines.Add(newRun() { Text = "This is some example text in bold" });
Italic itlText =new Italic();
itlText.Inlines.Add(newRun() { Text = "This is some example text in italics" });
Underline unText =new Underline();
unText.Inlines.Add(newRun() { Text = "This is some example text,underlined" });
BoldbldTextWithItalic = new Bold();
bldTextWithItalic.Inlines.Add(newItalic() { Inlines = { new Run(){ Text = "This is some example text,boldand italic" } } });
prgParagraph.Inlines.Add(bldText);
prgParagraph.Inlines.Add(newlineBreak());
prgParagraph.Inlines.Add(itlText);
prgParagraph.Inlines.Add(newlineBreak());
prgParagraph.Inlines.Add(unText);
prgParagraph.Inlines.Add(newlineBreak());
prgParagraph.Inlines.Add(bldTextWithItalic);
rbtMyRichTextBox.Blocks.Add(prgParagraph);
上面的代码分别实现了文本的加粗,倾斜,下滑线功能,和添加元素的代码没有太大区别。
添加InlineUIContainer:顾名思义,这个元素就是UI元素的容器,当我们想要在内容中添加Image,DataGrID这些元素的时候,它非常的方便,步骤也是和其它内联元素一样的。先创建一个InlineUIContainer,然后在容器中添加UIElement,把这个容器添加到Paragraph中
1. InlineUIContainer iuicContainer =newInlineUIContainer();
2. DataGrID dtgGrID =newDataGrID();
3. dtgGrID.autoGenerateColumns =true;
4. dtgGrID.WIDth = 400;
5. dtgGrID.Height = 200;
6. dtgGrID.ItemsSource = ...
7. iuicContainer.Child = dtgGrID;
8. Paragraph prgParagraph =newParagraph();
9. prgParagraph.Inlines.Add(iuicContainer);
10. rbtMyRichTextBox.Blocks.Add(prgParagraph);
InlineUIContaineriuicContainer = new InlineUIContainer();
DataGrID dtgGrID =new DataGrID();
dtgGrID.autoGenerateColumns= true;
dtgGrID.WIDth =400;
dtgGrID.Height =200;
dtgGrID.ItemsSource= ...
iuicContainer.Child= dtgGrID;
ParagraPHPrgParagraph = new Paragraph();
prgParagraph.Inlines.Add(iuicContainer);
rbtMyRichTextBox.Blocks.Add(prgParagraph);
上面的CODE如果改成XAML声明:
1. <RichTextBox AcceptsReturn="True"
2. margin="5"
3. x:name="rbtMyRichTextBox">
4. <Paragraph>
5. <InlineUIContainer>
6. <Image Source="Assets/logo.png" WIDth="100"></Image>
7. </InlineUIContainer>
8. </Paragraph>
9. </RichTextBox>
<RichTextBoxAcceptsReturn="True"
margin="5"
x:name="rbtMyRichTextBox">
<Paragraph>
<InlineUIContainer>
<ImageSource="Assets/logo.png" WIDth="100"></Image>
</InlineUIContainer>
</Paragraph>
</RichTextBox>
通过上面的内容已经对RichTextBox有了基本的了解,其实之前我们都是通过编程的方式加入这些元素,但是更为常见的场景是用户可以通过选择某些文本,并对选中的文本进行加粗等 *** 作,RichTextBox提供了一个类型为TextSelection的Selection属性,它包含了当前被选择的文本,然后我们可以通过GetPropertyValue和ApplyPropertyValue方法对选择的文本进行 *** 作。
*** 作Selection:
1. if(rbtMyRichTextBox !=null&& rbtMyRichTextBox.Selection.Text.Length > 0)
2. {
3. if (rbtMyRichTextBox.Selection.GetPropertyValue(Run.FontWeightProperty)is FontWeight
4. &&((FontWeight)rbtMyRichTextBox.Selection.GetPropertyValue(Run.FontWeightProperty))== FontWeights.normal)
5. {
6. rbtMyRichTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty,FontWeights.Bold);
7. }
8. else
9. {
10. rbtMyRichTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty,FontWeights.normal);
11. }
12. }
13. if(rbtMyRichTextBox !=null)
14. {
15. rbtMyRichTextBox.Focus();
16. }
if (rbtMyRichTextBox !=null && rbtMyRichTextBox.Selection.Text.Length > 0)
{
if(rbtMyRichTextBox.Selection.GetPropertyValue(Run.FontWeightProperty) isFontWeight
&&((FontWeight)rbtMyRichTextBox.Selection.GetPropertyValue(Run.FontWeightProperty))== FontWeights.normal)
{
rbtMyRichTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty,FontWeights.Bold);
}
else
{
rbtMyRichTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty,FontWeights.normal);
}
}
if (rbtMyRichTextBox != null)
{
rbtMyRichTextBox.Focus();
}
上面的代码中,我们获取了选中文本的FontWeight属性,通过检查它是normal还是Bold改变其值,这里的逻辑也可以用在 FontSize等其它的属性上。
简单的复制,剪切,粘贴功能:Sl4支持Clipboard功能,所以通过访问Clipboard我们可以很容易实现剪切,复制功能。
1. privatevoIDcopy_Click(object sender,RoutedEventArgs e)
2. {
3. Clipboard.SetText(rbtMyRichTextBox.Selection.Text);
4. rbtMyRichTextBox.Focus();
5. }
6. privatevoIDCut_Click(object sender,RoutedEventArgs e)
7. {
8. Clipboard.SetText(rbtMyRichTextBox.Selection.Text);
9. rbtMyRichTextBox.Selection.Text ="";
10. rbtMyRichTextBox.Focus();
11. }
12. //上面的代码分别演示如何对RichTextBox中被选中的文本进行复制,剪切
13. privatevoIDPaste_Click(object sender,248)">14. {
15. rbtMyRichTextBox.Selection.Text =Clipboard.GetText();
16. rbtMyRichTextBox.Focus();
17. }
private voIDcopy_Click(object sender,RoutedEventArgs e)
{
Clipboard.SetText(rbtMyRichTextBox.Selection.Text);
rbtMyRichTextBox.Focus();
}
private voIDCut_Click(object sender,RoutedEventArgs e)
{
Clipboard.SetText(rbtMyRichTextBox.Selection.Text);
rbtMyRichTextBox.Selection.Text ="";
rbtMyRichTextBox.Focus();
}
//上面的代码分别演示如何对RichTextBox中被选中的文本进行复制,剪切
private voIDPaste_Click(object sender,RoutedEventArgs e)
{
rbtMyRichTextBox.Selection.Text = Clipboard.GetText();
rbtMyRichTextBox.Focus();
}
复制的时候,如果RichTextBox没有选择任何元素,那么剪切板上的文本将显示在鼠标的当前位置上。注意的是,Cilpboard只能保护简单的文本,所以当粘贴,复制的时候会丢失之前格式化的信息。
保存内容为文件:RichTextBox提供了一个名为Xaml的属性,通过这个属性可以很方便的将内容保存为文件。
1. privatevoIDSave_Click(object sender,248)">2. {
3. var uIElements = from blockinrbtMyRichTextBox.Blocks
4. from inlinein (blockasParagraph).Inlines
5. where inline.GetType() ==typeof(InlineUIContainer)
6. select inline;
7. if(uIElements.Count() != 0)
8. {
9. MessageBox.Show("UIElementscannot be saved");
10. return;
11. }
12. SavefileDialog sfdSaveXaml =newSavefileDialog();
13. sfdSaveXaml.DefaultExt =".sav";
14. sfdSaveXaml.Filter ="Savedrtb files|*.rtb";
15. if(sfdSaveXaml.ShowDialog().Value)
16. {
17. using (fileStreamfs = (fileStream)sfdSaveXaml.Openfile())
18. {
19. System.Text.UTF8EnCoding enc =newSystem.Text.UTF8EnCoding();
20. byte[] buffer= enc.GetBytes(rbtMyRichTextBox.Xaml);
21. fs.Write(buffer,buffer.Length);
22. fs.Close();
23. }
24. }
25. }
private voIDSave_Click(object sender,RoutedEventArgs e)
{
var uIElements = from block inrbtMyRichTextBox.Blocks
from inline in (block asParagraph).Inlines
where inline.GetType() ==typeof(InlineUIContainer)
select inline;
if (uIElements.Count() != 0)
{
MessageBox.Show("UIElementscannot be saved");
return;
}
SavefileDialog sfdSaveXaml = newSavefileDialog();
sfdSaveXaml.DefaultExt =".sav";
sfdSaveXaml.Filter = "Saved rtbfiles|*.rtb";
if (sfdSaveXaml.ShowDialog().Value)
{
using (fileStream fs =(fileStream)sfdSaveXaml.Openfile())
{
System.Text.UTF8EnCoding enc =new System.Text.UTF8EnCoding();
byte[] buffer =enc.GetBytes(rbtMyRichTextBox.Xaml);
fs.Write(buffer,buffer.Length);
fs.Close();
}
}
}
因为RichTextBox不支持保存InlineUIContainer的元素,所以上面的代码中先将其排除,然后通过fileStream保存
打开文件:这个与保存文件过程是相反的,只要将RichTextBox的Xaml属性设置为读出的XAML。
1. privatevoIDOpen_Click(object sender,248)">2. {
3. OpenfileDialog ofdOpenXaml =newOpenfileDialog();
4. ofdOpenXaml.Multiselect =false;
5. ofdOpenXaml.Filter ="Savedrtb files|*.rtb";
6. if (ofdOpenXaml.ShowDialog().Value)
7. {
8. fileInfo fiXamlfile = ofdOpenXaml.file;
9. StreamReader sr = fiXamlfile.OpenText();
10. rbtMyRichTextBox.Xaml = sr.ReadToEnd();
11. sr.Close();
12. }
13. }
private voIDOpen_Click(object sender,RoutedEventArgs e)
{
OpenfileDialog ofdOpenXaml = newOpenfileDialog();
ofdOpenXaml.Multiselect = false;
ofdOpenXaml.Filter = "Saved rtbfiles|*.rtb";
if (ofdOpenXaml.ShowDialog().Value)
{
fileInfo fiXamlfile =ofdOpenXaml.file;
StreamReader sr =fiXamlfile.OpenText();
rbtMyRichTextBox.Xaml =sr.ReadToEnd();
sr.Close();
}
}
上面的保存,读取的文件只支持rtb格式的,其实可以使用其它的格式,当然肯定需要额外的编码,比如你打开一个文本文件,那么首先需要你创建所必须的Run标记。总之,你需要将内容转换成Xaml格式。对于保存文件,可以看看CodePlex上一个开眼项目:WordToXaml这篇文章是参考的国外这篇RichTextBox,文中的源码可以在其中找到。
1.13. SavefileDialog
后台代码:
SavefileDialogsd = new SavefileDialog();
sd.Filter = "文本格式|*.txt";
sd.DefaultExt = "txt";
bool?result = sd.ShowDialog();
if(result == true)
{
System.IO.Stream fileStream = sd.Openfile();
System.IO.StreamWriter sw =newSystem.IO.StreamWriter(fileStream);
sw.Writeline("中华人民共和国.");
sw.Flush();
sw.Close();
}
1.14. Scrollbar
前台代码:
<Scrollbar OrIEntation="Vertical" Height="220" HorizontalAlignment="left" margin="60,35,0" name="Vscrlb"VerticalAlignment="top" WIDth="18" Maximum="100" Value="50" />
<Scrollbar OrIEntation="Horizontal" Height="22" HorizontalAlignment="left" margin="93,12,0" name="Hscrlb" VerticalAlignment="top" WIDth="422" Value="0" Maximum="1000" /
1.15. ScrollVIEwer
前台代码:
<ScrollVIEwer Height="121" Background="Blue" HorizontalAlignment="left" margin="132,304,0" name="scrollVIEwer1"VerticalAlignment="top" WIDth="383">
<TextBox Height="107" name="textBox1" WIDth="326" />
</ScrollVIEwer>
1.16. SlIDer
<SlIDer Height="23" HorizontalAlignment="left" margin="182,251,0" name="slIDer1" VerticalAlignment="top" WIDth="357" Maximum="1000" Value="80" OrIEntation="Horizontal" />
1.17. StackPanel
<StackPanel x:name="st1" Height="300" WIDth="400" >
<button Height="90" WIDth="129" ></button>
<Ellipse Height="100" name="ellipse1" stroke="Black" strokeThickness="1" WIDth="200" />
</StackPanel>
1.18. TabControl
<sdk:TabControl Height="100" name="tabControl1" WIDth="200">
<sdk:TabItem header="tabItem1" name="tabItem1">
<GrID />
</sdk:TabItem>
<sdk:TabItem header="tabItem2" name="tabItem2">
<GrID />
</sdk:TabItem>
<sdk:TabItem header="tabItem3" name="tabItem3">
<GrID />
</sdk:TabItem>
</sdk:TabControl>
1.19. TextBlock
<TextBlock Height="23" HorizontalAlignment="left" margin="124,78,0" name="textBlock1" Text="TextBlock"VerticalAlignment="top" />
1.20. TextBox
<TextBox Height="23" HorizontalAlignment="left" margin="110,160,0" name="textBox1"VerticalAlignment="top" WIDth="120">
<TextBox.borderBrush>
<linearGradIEntBrush>
<GradIEntStop color="#FFA3AEB9" Offset="0" />
<GradIEntStop color="#FF8399A9" Offset="0.375" />
<GradIEntStop color="#FF718597" Offset="0.375" />
<GradIEntStop color="#FF2E9CEF" Offset="1" />
</linearGradIEntBrush>
</TextBox.borderBrush>
</TextBox>
总结以上是内存溢出为你收集整理的Silverlight之控件应用总结(二)(4)全部内容,希望文章能够帮你解决Silverlight之控件应用总结(二)(4)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)