欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF中的數(shù)據(jù)模板用法介紹

 更新時間:2022年01月28日 09:17:08   作者:痕跡g  
這篇文章介紹了WPF中的數(shù)據(jù)模板用法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

數(shù)據(jù)模板常用在3種類型的控件, 下圖形式:

  • 1.Grid這種列表表格中修改Cell的數(shù)據(jù)格式, CellTemplate可以修改單元格的展示數(shù)據(jù)的方式。
  • 2.針對列表類型的控件, 例如樹形控件,下拉列表,列表控件, 可以修改其中的ItemTemplate。
  • 3.修改ContentTemplate, 例UserControl控件的數(shù)據(jù)展現(xiàn)形式。

CellTemplate 模板

下面用一個例子, 來演示CellTemplate使用。例子實現(xiàn)一個DataGrid 展示一個普通的數(shù)據(jù)標, 同時新增一列CellTemplate添加兩個自定義的按鈕, 如下圖所示。

            <DataGrid Name="gd" AutoGenerateColumns="False" CanUserSortColumns="True"  CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding UserName}" Width="100" Header="學生姓名"/>
                    <DataGridTextColumn Binding="{Binding ClassName}" Width="100" Header="班級名稱"/>
                    <DataGridTextColumn Binding="{Binding Address}" Width="200" Header="地址"/>
                    <DataGridTemplateColumn Header="操作" Width="100" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left">
                                    <Button Content="編輯"/>
                                    <Button Margin="8 0 0 0" Content="刪除" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

完成操作, 然后后臺進行該DataGrid進行綁定數(shù)據(jù), 查詢綁定后的效果。

            List<Student> students = new List<Student>();
            students.Add(new Student() { UserName = "小王", ClassName = "高二三班", Address = "廣州市" });
            students.Add(new Student() { UserName = "小李", ClassName = "高三六班", Address = "清遠市" });
            students.Add(new Student() { UserName = "小張", ClassName = "高一一班", Address = "深圳市" });
            students.Add(new Student() { UserName = "小黑", ClassName = "高一三班", Address = "贛州市" });
            gd.ItemsSource = students;

最終的效果, 在數(shù)據(jù)的表格最后一列, 將會在一列中分別生成 兩個普通按鈕。

ItemTemplate

在列表的控件中, 常常會出現(xiàn)一些需求, 類似在下拉控件或樹控件中添加一個 CheckBox選擇框, 一個圖標或圖片, 這個時候, 我們就可以利用自定義的DataTemplate 來實現(xiàn)這個功能,

接下來, 用一個示例來簡單演示其功能, 同樣, 該例子演示利用 ListBox 和 ComboBox來綁定一個 顏色代碼列表, 同時展示其顏色。

    <Window.Resources>
        <DataTemplate x:Key="comTemplate">
            <StackPanel Orientation="Horizontal" Margin="5,0">
                <Border Width="10" Height="10" Background="{Binding Code}"/>
                <TextBlock Text="{Binding Code}" Margin="5,0"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <ComboBox Name="cob" Width="120" Height="30" ItemTemplate="{StaticResource comTemplate}"/>
            <ListBox Name="lib" Width="120" Height="100" Margin="5,0"  ItemTemplate="{StaticResource comTemplate}"/>
        </StackPanel>
    </Grid>

上面的代碼中, 定義了一個DataTemplate , 頂一個 長寬10px的border用于顯示顏色代碼, 綁定到Border背景顏色上, 定義了一個TextBlock用于展示顏色的代碼。

下面為后臺的綁定代碼

            List<Color> ColorList = new List<Color>();
            ColorList.Add(new Color() { Code = "#FF8C00" });
            ColorList.Add(new Color() { Code = "#FF7F50" });
            ColorList.Add(new Color() { Code = "#FF6EB4" });
            ColorList.Add(new Color() { Code = "#FF4500" });
            ColorList.Add(new Color() { Code = "#FF3030" });
            ColorList.Add(new Color() { Code = "#CD5B45" });

            cob.ItemsSource = ColorList;
            lib.ItemsSource = ColorList;

最終的測試效果如下所示:

ItemsControl

定義ItemsControl 主要分兩個步驟:

  • 1.設置ItemsPanel容器, 用于容納列表的最外層容器
  • 2.定義子項的DataTemplate
           <ItemsControl Name="ic">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Width="50" Height="50" Content="{Binding Code}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

上面代碼中, 定義了一個WarpPanel 容器為ItemControl的 最外層容器, 子項數(shù)據(jù)模板則綁定了一個按鈕, 后臺代碼綁定幾條數(shù)據(jù), 查看其效果: 橫排排列五個按鈕, 內(nèi)容分別是 1~6.

            List<Test> tests = new List<Test>();
            tests.Add(new Test() { Code = "1" });
            tests.Add(new Test() { Code = "2" });
            tests.Add(new Test() { Code = "3" });
            tests.Add(new Test() { Code = "4" });
            tests.Add(new Test() { Code = "6" });
            ic.ItemsSource = tests;

查看ItemsControl可視化樹的結(jié)構(gòu)組成?

剖析該結(jié)構(gòu), 可以看到, 紫色的1處, 為最外層的WrapPanel容器, 用于容納排列按鈕, 由于該示例設置了 Orientation="Horizontal" , 所以按鈕則按水平排列, 再看 橘色 2處, 可以看見子項外層由一個內(nèi)容呈現(xiàn)包括著, 內(nèi)容為一個按鈕, 由于綁定搞得數(shù)據(jù)是5個, 所以分別生成了內(nèi)容為1~6的5個按鈕。

說明: 那是不是以為則ItemsPanel 放置任何元素都行? 很明顯是不行的。 ItemsPanel的容器需要滿足一個條件, 則是屬于Panel族的元素, 否則會提示以下錯誤:

關(guān)于每種元素的分類可以看關(guān)于控件介紹的文章: http://www.dbjr.com.cn/article/236066.htm

ContentTemplate

長話短說, 這個東西用的太少了, 詳細的可以搜索一下相關(guān)的使用資料。

本章測試代碼下載

到此這篇關(guān)于WPF中的數(shù)據(jù)模板用法介紹的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解asp.net core 依賴注入

    詳解asp.net core 依賴注入

    這篇文章主要介紹了詳解asp.net core 依賴注入的相關(guān)知識,文中講解非常詳細,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06
  • WPF框架Prism中導航Navigation用法介紹

    WPF框架Prism中導航Navigation用法介紹

    這篇文章介紹了WPF框架Prism中導航Navigation的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • MVC中Action方法的返回類型介紹

    MVC中Action方法的返回類型介紹

    這篇文章介紹了MVC中的Action方法的返回類型,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • ASP.NET?Core?MVC路由(Routing)的用法

    ASP.NET?Core?MVC路由(Routing)的用法

    這篇文章介紹了ASP.NET?Core?MVC路由(Routing)的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • ASP.NET中Application、Cookie、Session、Cache和ViewState

    ASP.NET中Application、Cookie、Session、Cache和ViewState

    本文主要介紹Application、Cookie、Session、Cache和ViewState,并總結(jié)了在什么情況下使用他們,希望對大家有所幫助。
    2016-04-04
  • ABP基礎(chǔ)架構(gòu)深入探索

    ABP基礎(chǔ)架構(gòu)深入探索

    這篇文章主要介紹了ABP基礎(chǔ)架構(gòu)深入探索,為了了解應用程序是如何配置和初始化,本文將探討ASP.NET?Core和ABP框架最基本的構(gòu)建模塊
    2022-05-05
  • ASP.NET處理HTTP請求的流程:IHttpModule、IHttpHandler與管道事件

    ASP.NET處理HTTP請求的流程:IHttpModule、IHttpHandler與管道事件

    這篇文章介紹了ASP.NET處理HTTP請求的流程:IHttpModule、IHttpHandler與管道事件,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Visual Studio快速開發(fā)以及Visual Studio 2010新功能介紹

    Visual Studio快速開發(fā)以及Visual Studio 2010新功能介紹

    一直以來都在摸索著vb6.0的一些用法和語法,趁這次培訓的機會正好整理Visual Studio的一些快速開發(fā)的技巧,還有一些vs2010的新功能收集和體會,把培訓的一些文檔性質(zhì)的記錄下來,希望對各位有用
    2011-12-12
  • 幾個C#常用正則表達式的總結(jié)

    幾個C#常用正則表達式的總結(jié)

    幾個C#常用正則表達式的總結(jié)...
    2006-08-08
  • WCF入門需要掌握的基礎(chǔ)知識

    WCF入門需要掌握的基礎(chǔ)知識

    這篇文章介紹了WCF入門需要掌握的基礎(chǔ)知識,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-01-01

最新評論