關(guān)于做網(wǎng)站的策劃書電商平臺(tái)排行榜
1:控件模版簡介:
自定義控件模版:自己添加的樣式、標(biāo)簽,控件模版也是屬于資源的一種,
? ? ? ? 每一個(gè)控件模版都有一唯一的 key,在控件上通過template屬性進(jìn)行綁定
什么場景下使用自定義控件模版,當(dāng)項(xiàng)目里面多個(gè)地方使用到相同效果,這時(shí)候可以把相同
? ? ? ? 效果封裝成一個(gè)自定義模版,例如項(xiàng)目好幾個(gè)地方需要一個(gè)弧度并且鼠標(biāo)放上去效果是紅色等按鈕。就可以
? ? ? ? 把按鈕從新自定義一下。
2:關(guān)于控件模版的實(shí)例
<Window.Resources><!--自定義模版--><ControlTemplate x:Key="c1" TargetType="Button" ><Border Background="AliceBlue"CornerRadius="5"BorderThickness="2"x:Name="border"><!--ContentPresenter 呈現(xiàn)內(nèi)容的標(biāo)簽--><StackPanel Orientation="Horizontal"><TextBlock VerticalAlignment="Center"Margin="0,0,10,0"Name="t1"Text="☆"></TextBlock><ContentPresenter HorizontalAlignment="Center"VerticalAlignment="Center"></ContentPresenter> </StackPanel></Border><!--Triggers 設(shè)置觸發(fā) 鼠標(biāo)移去 鼠標(biāo)移開等效果--><ControlTemplate.Triggers><!--Property 設(shè)置的屬性Value 屬性值--><!--IsMouseOver 鼠標(biāo)放上去TargetName="border" 目標(biāo)元素的name屬性--><Trigger Property="IsMouseOver"Value="true"><Setter Property="Background"Value="red"TargetName="border"></Setter><Setter Property="BorderBrush"Value="green"TargetName="border"></Setter><Setter Property="Text"Value="★"TargetName="t1"></Setter></Trigger></ControlTemplate.Triggers></ControlTemplate>
</Window.Resources><Grid><!--WPF不僅支持傳統(tǒng)winfrom編程,并且還引入以模版為核心的新一代設(shè)計(jì)理念,在wpf通過使用模版將數(shù)據(jù)和界面進(jìn)行解耦。模版主要分為倆大類型的模版:數(shù)據(jù)模版【DataTemplate】 和控件模版【Control Template】,控件模版:描述如何顯示控件,數(shù)據(jù)模版:描述如何顯示數(shù)據(jù),--><!--<Button Width="100" Height="40" Content="hello world"></Button>--><Button Template="{StaticResource c1}" Width="100" Height="40" Content="刪除" ></Button><Button Template="{StaticResource c1}"Width="100"Height="40"Content="編輯"Margin="0,0,0,100"></Button></Grid>
效果圖如下
?1關(guān)于數(shù)據(jù)模板的簡介:
數(shù)據(jù)模版 DataTemplate:決定了數(shù)據(jù)展示形式和用戶體驗(yàn),在控件上通過使用ItemTemplate
? ? ? ? 屬性進(jìn)行模版的綁定 ?ItemTemplate="{StaticResource c1}
控件模版 ControlTemplate:設(shè)置控件展示,在控件上通過使用Template屬性進(jìn)行模版綁定
? ? ? ? Template="{StaticResource c1}
2 關(guān)于它的實(shí)例
<Window.Resources><DataTemplate x:Key="c1"><StackPanel Orientation="Horizontal"><Border Width="10" Height="10"Background="{Binding Code}"></Border><TextBlock Text="{Binding Code}"> </TextBlock></StackPanel></DataTemplate></Window.Resources><Grid><ListBox Width="200"Height="100"HorizontalAlignment="Left"VerticalAlignment="Top"ItemTemplate="{StaticResource c1}"Name="l1"></ListBox><ComboBox Width="200"Height="40"ItemTemplate="{StaticResource c1}"Name="com"></ComboBox><!--這是之前的itemsource的寫法--><ListBox Width="200"Height="100"HorizontalAlignment="Left"VerticalAlignment="Top"ItemsSource="{Binding}"Margin="300,0,0,0"Name="l3"><ListBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Border Width="10"Height="10"Background="{Binding Code}"></Border><TextBlock Text="{Binding Code}"></TextBlock></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></Grid>
定義模型類
public Window數(shù)據(jù)模版(){InitializeComponent();List<Color> list = new List<Color>(); //數(shù)據(jù)源集合list.Add(new Color() { Code = "#ff0000"});list.Add(new Color(){Code = "#00ff00"});list.Add(new Color(){Code = "#00FF00"});list.Add(new Color(){Code = "#55efc4"});list.Add(new Color(){Code = "#FBCA11"});this.l1.ItemsSource = list;this.com.ItemsSource = list;//之前的數(shù)據(jù)綁定的寫法this.l3.ItemsSource = list;// this.l3.DisplayMemberPath = "Code";}
}// 模型類
public class Color
{public string Code { get; set; }//顏色的取值#FF0000
}