引言:在進(jìn)行WPF項目開(kāi)發(fā)過(guò)程中,由于項目的需要,經(jīng)常要對某個(gè)控件進(jìn)行特殊的設定,其中就牽涉到模板的相關(guān)方面的內容。本文也是在自己進(jìn)行項目開(kāi)發(fā)過(guò)程中遇到控件模板設定時(shí)集中搜集資料后整理出來(lái)的,以供在以后的項目開(kāi)發(fā)過(guò)程中查閱。WPF有控件模板和數據模板,從字面上來(lái)看,控件模板主要是用來(lái)改變控件的外觀(guān),數據模板則定義控件中數據的表現方式。下面讓逐一進(jìn)行介紹。
控件模板ControlTemplate,有兩部分:VistualTree視覺(jué)樹(shù),即是能看到的外觀(guān);Trigger觸發(fā)器,里面包括外部條件達到某一條件下會(huì )引起的響應。
參考代碼:
<Button Content="Button" Grid.Row="1" Height="136" HorizontalAlignment="Left" Margin="114,80,0,0" Name="button1" VerticalAlignment="Top" Width="205" > <Button.Template > <ControlTemplate > <Grid > <Ellipse Name="faceEllipse" Height="50" Width="100" Fill="{TemplateBinding Button.Background}"/> <TextBlock Name="txtBlock" /> </Grid > <ControlTemplate.Triggers > <Trigger Property="Button.IsMouseOver" Value="True"> <Setter Property="Button.Background" Value="blue"/> </Trigger > </ControlTemplate.Triggers > </ControlTemplate > </Button.Template > </Button >在上面的前臺代碼中,包含button控件的視覺(jué)樹(shù)和觸發(fā)器。Grid部分是改變button控件的視覺(jué)樹(shù)部分,意思是將button控件顯示部分橢圓,而背景色是控件的原本色調;Triggers部分是當有鼠標在button控件上面是控件的背景色變?yōu)樗{色。
為了便于多次利用,可以將其寫(xiě)入模板中,如下:
<Window.Resources > <ControlTemplate x:Key="buttonTemplate" TargetType="Button" > <Grid > <Ellipse Name="faceEllipse" Height="50" Width="100" Fill="{TemplateBinding Button.Background}"/> <TextBlock Name="txtBlock" /> </Grid > <ControlTemplate.Triggers > <Trigger Property="Button.IsMouseOver" Value="True"> <Setter Property="Button.Background" Value="blue"/> </Trigger > </ControlTemplate.Triggers > </ControlTemplate > </Window.Resources >調用時(shí):
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="216,224,0,0" Name="button3" Width="75" Template="{StaticResource buttonTemplate}"/>DataTemplate模板:
參考代碼:
<ListBox Height="202" HorizontalAlignment="Left" Margin="21,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="384" > <ListBox.ItemTemplate > <DataTemplate > <StackPanel Orientation="Horizontal" > <TextBox Width="60" Text="{Binding Path=Name}"/> <TextBox Width="60" Text="{Binding Path=ID}"/> <TextBox Width="60" Text="{Binding Path=Age}"/> </StackPanel > </DataTemplate > </ListBox.ItemTemplate > </ListBox >上例是將listbox作為實(shí)例來(lái)做展示,在一個(gè)listbox控件中為了顯示多行和多列數據,使用ItemTemplate進(jìn)行構造。
WPF中的style:style,樣式風(fēng)格的意思,簡(jiǎn)單來(lái)說(shuō)就是對屬性值的批處理,在實(shí)際使用過(guò)程中幫助非常大。
參考代碼:
<Window.Resources > <ResourceDictionary > <Style x:Key="dgButton" TargetType="Button" > <Setter Property="Background" Value="Blue" /> <Setter Property="FontSize" Value="20"/> </Style > <Style x:Key="cb" TargetType="CheckBox" > <Style.Triggers > <Trigger Property="IsChecked" Value="True"> <Setter Property="FontSize" Value=" 40"/> <Setter Property="Foreground" Value="Red" /> </Trigger > </Style.Triggers > </Style > </ResourceDictionary > </Window.Resources >調用方式:
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="216,224,0,0" Name="button3" VerticalAlignment="Top" Width="75" Style ="{StaticResource dgbutton}"/><CheckBox Content="CheckBox" Height="58" HorizontalAlignment="Left" Margin="654,16,0,0" Name="checkBox1" VerticalAlignment="Top" Width="175" Style="{StaticResource cb}" Grid.Row="1" />上述代碼有兩個(gè)組成部分:
1 設置button的的背景色和字體大小,說(shuō)到底也是對button的屬性進(jìn)行批量處理。當然在實(shí)際使用button控件時(shí)也可單獨使用,此處只是便于處理。
2 設置checkbox的觸發(fā)器,當對check進(jìn)行選擇是,字體和背景色都會(huì )做出改變。
總結:在項目開(kāi)發(fā)過(guò)程中,經(jīng)常使用的也就是這些了,如果有更為特殊需求,那就需要另外尋求方案處理了。
聯(lián)系客服