1. 程式人生 > 程式設計 >WPF常用控制元件用法及介紹

WPF常用控制元件用法及介紹

1.GroupBox

WPF常用控制元件用法及介紹

注意: GroupBox仍然需要佈局容器來放置元素。如: StackPanel面板

<GroupBox Header="select number?">
       <StackPanel>
           <RadioButton>one</RadioButton>
           <RadioButton>two</RadioButton>
           <RadioButton>three</RadioButtohttp://www.cppcns.comn>
       </StackPanel>
   </GroupBox>

2.TabControl

像這種標籤頁控制元件,在winform種非常常見,Tabpge子頁面,而在WPF種,對應的則是TabItem類。

WPF常用控制元件用法及介紹

所示的程式碼示意圖:

<TabControl>
  www.cppcns.com      <TabItem Header="首頁">
            <StackPanel>
                <Button>button1</Button>
                <Button>button2</Button>
                <Button>button3</Button>
            </StackPanel>
        </TabItem>
 
        <TabItem Header="第二頁">
            <StackPanel>
                <Button>button4</Button>
                <Button>button5</Button>
                <Button>button6</Button>
            </StackPanel>
        </TabItem>
         
    </TabControl>

與Content屬性相同,TabItem的Header同樣可以接收任何型別的物件。這意味著可以建立一個組合框或選項卡。在他們的頁標題中包含任意圖形和任意元素。如下所示:

WPF常用控制元件用法及介紹

<TabControl>
        <TabItem >
            <TabItem.Header>
                <StackPanel Orientation="Horizontal">
                    <Button Background="Transparent" BorderBrush="Transparent">♥</Button>
                    <TextBox BorderBrush="Transparent">首頁</TextBox>
                </StackPanel>
            </TabItem.Header>
 
            <StackPanel>
                <Button>button1</Button>
                <Button>button2</Button>
                <Button>button3</Button>
            </StackPanel>
 
        <CNMoY
/TabItem> </TabControl>

3.Expander選單控制元件

具備標題的內容收縮控制元件,在web中很普遍,用於左側選單。

WPF常用控制元件用法及介紹

程式碼如下所示:

<StackPanel>
    <Expander Header="One" Margin="5" Padding="5">
        <TextBlock TextWrapping="Wrap">Ofo has been favoredwww.cppcns.com by the public,even the foreign people speak highly of it. These yellow
            bikes can be found everywhere,so the people who are in a hurry can
            use it and then reached the destination in time.</TextBlock>
    </Expander>
 
    <Expander Header="Two" Margin="5" Padding="5">
        <TextBlock TextWrapping="Wrap">Especially for the visitors,they can ride these bikes and then have a look at the scenery around.</TextBlock>
    </Expander>
 
    <Expander Header="Three" Margin="5" Padding="5">
        <TextBlock TextWrapping="Wrap">It can saves them a lot of money and the most important thing is the convenience it brings.</TextBlock>
    </Expander>
 
</StackPanel>

4.ListBox控制元件

ListBox控制元件是一個非常靈活的控制元件,它不僅包含子元素ListBoxItem物件。而且也可以駐留其他元素,這也就是ListBoxItem類繼承於ContentControl類,從而ListBoxItem能夠包含一段巢狀的內容。

例如,建立一個包含普通按鈕的列表。如下所示:

WPF常用控制元件用法及介紹

<StackPanel>
       <ListBox>
           <ListBoxItem>
               <Button>♥ button1</Button>
           </ListBoxItem>
       </ListBox>
 
       <ListBox>
           <ListBoxItem>
               <Button>♥ button2</Button>
           </ListBoxItem>
       </ListBox>
 
       <ListBox>
           <ListBoxItem>
               <Button>♥ button3</Button>
           </ListBoxItem>
       </ListBox>
   </StackPanel>

5. ProgressBar進度條

IsIndeterminate屬性設定為True,控制元件則會週期性的顯示一個綠色(預設)從左到右的脈衝。

<Grid>
    <ProgressBar IsIndeterminate="True" Height="30"></ProgressBar>
</Grid>

當然,我們也可以通過修改Foreground 屬性,修改其滾動的顏色。如下所示

WPF常用控制元件用法及介紹

<StackPanel>
    <ProgressBar Margin="2" IsIndeterminate="True" Height="30" Foreground="Green" ></ProgressBar>
    <ProgressBar Margin="2" IsIndeterminate="True" Height="30" Foreground="Red" ></ProgressBar>
    <ProgressBar Margin="2" IsIndeterminate="True" Height="30" Foreground="Chocolate" ></ProgressBar>
    <ProgressBar Margin="2" IsIndeterminate="True" Height="30" Foreground="DarkSalmon" ></ProgressBar>
    <ProgressBar Margin="2" IsIndeterminate="True" Height="30" Foreground="Pink" ></ProgressBar>
    <ProgressBar Margin="2" IsIndeterminate="True" Height="30" Foreground="DodgerBlue" ></ProgressBar>
</StackPanel>

6.日期控制元件

WPF中有兩個日期控制元件,Calendar和DatePicker,前者是以一個日曆的形式,後者則像是一個文字框的形式。

WPF常用控制元件用法及介紹

<StackPanel>
      <DatePicker></DatePicker>
      <Calendar></Calendar>
  </StackPanel>

同時,控制元件還具備眾多屬性。

DisplayDateStart

DisplayDateEnd

設定在日曆檢視中顯示的日期範闈,從第一個妯早的日期(DisplayDateStart)到最後最近

的日期(DisplayDateEnd),使用者不能導航到沒打包含能夠顯示的日期的月份。為了顯示所

有日期,可以將DisplayDateStart屬性設定為DateTime.MinValue,並將DisplayDateEnd

W性設定為DateTime.MaxValue

BlackoutDates

儲存在日曆中將被禁用或不能選擇的日期集合?如果這呰日期不在可以顯示的曰期範ra

內,或芥如果己經選擇了這些曰期中的蓯個日期,將接收到一個異常,為了阻止選擇任

何過去的日期,可以呼叫BlackoutDates.AddDatcsInPast()方法

SelectedDate

作為一個DateTime物件提供選擇的日期(或名?沒冇日期被選中時使用null值)。可以通過

程式碼、通過單擊日曆中的日期、或通過使用者鍵入一個日期字元審(在DatePicker控制元件中>

設定該屬性.在日曆檢視中,選抒的日期使用一個具有陰影的方框標識,只有當曰期控

件具有焦點時才會顯示該方框

SelectedDates作為 DateTime 物件的集合提供選擇的日期。 Calcndar 控制元件支援該屬性,並目.只有當修改了 SelectionMode 屬性,以允許選擇多個日期時,該屬性才有用
DispalyDate(使用Datetime 物件確定在日曆檢視中最初顯示的日期.如果該屬性為空,顯示selectedDate屬性的值。如果 DisPlayDate 和 select 曰 LDate 屬性均為空,使用當前日期.顯示的日期決定了日曆檢視中最初的月份頁面。當日期控制元件具有焦點時,在該月份中恰當的某天周圍顯示一個方形邊框(該邊框和用於當前選擇日期的陰影方框是有區別的)
FristDayOfweek確定在日曆中每行的開始位置(最左邊)顯示一星期中的哪一天
IsToDayHighighted確定日曆檢視是否通過突出顯示指出當前日期

到此這篇關於WPF常用控制元件用法及介紹的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援我們。