WPF的Datagrid顯示,樣式(隔行換色)

2021-07-15 06:05:57 字數 4533 閱讀 2732

主要寫一些最近一段時間學習wpf的顯示

直接顯示與.net中的gridview有些相似,直接把乙個list扔入datagrid中就可以顯示,都可以自動的把物件的屬性載入到頁面上。

mainwindow.xaml.cs

[html]view plain

copy

public mainwindow()  

private void window_loaded(object sender, routedeventargs e)  

this.grid_user.itemssource

= list

;  }  

}  public class user  

public string name   

}  

mainwindow.xaml   alternationcount="2"隔行換色

[html]view plain

copy

<

grid

>

<

datagrid

name

="grid_user"

isreadonly

="true"

alternationcount

="2"

>

<

datagrid.columns

>

<

datagridtextcolumn

header

="id"

width

="50"

binding=""

/>

<

datagridtextcolumn

header

="name"

width

="50"

binding=""

/>

datagrid.columns

>

datagrid

>

grid

>

自定義datagrid的樣式

[html]view plain

copy

<

resourcedictionary

xmlns

=""xmlns:x

="">

<

style

targettype

="datagrid"

>

<

setter

property

="canuserresizecolumns"

value

="false"

/>

<

setter

property

="background"

value

="#e6dbbb"

/>

<

setter

property

="borderbrush"

value

="#d6c79b"

/>

<

setter

property

="horizontalgridlinesbrush"

>

<

setter.value

>

<

solidcolorbrush

color

="#d6c79b"

/>

setter.value

>

setter

>

<

setter

property

="verticalgridlinesbrush"

>

<

setter.value

>

<

solidcolorbrush

color

="#d6c79b"

/>

setter.value

>

setter

>

style

>

<

style

targettype

="datagridrow"

>

<

setter

property

="background"

value

="#f2f2f2"

/>

<

setter

property

="height"

value

="25"

/>

<

setter

property

="foreground"

value

="black"

/>

<

style.triggers

>

<

trigger

property

="alternationindex"

value

="0"

>

<

setter

property

="background"

value

="#e7e7e7"

/>

trigger

>

<

trigger

property

="alternationindex"

value

="1"

>

<

setter

property

="background"

value

="#f2f2f2"

/>

trigger

>

<

trigger

property

="ismouseover"

value

="true"

>

<

setter

property

="background"

value

="lightgray"

/>

trigger

>

<

trigger

property

="isselected"

value

="true"

>

<

setter

property

="foreground"

value

="black"

/>

trigger

>

style.triggers

>

style

>

<

style

targettype

="datagridcell"

>

<

setter

property

="template"

>

<

setter.value

>

<

controltemplate

targettype

="datagridcell"

>

<

textblock

textalignment

="center"

verticalalignment

="center"

>

<

contentpresenter

/>

textblock

>

controltemplate

>

setter.value

>

setter

>

<

style.triggers

>

<

trigger

property

="isselected"

value

="true"

>

<

setter

property

="background"

value

="white"

/>

<

setter

property

="borderthickness"

value

="0"

/>

-->

<

setter

property

="foreground"

value

="black"

/>

trigger

>

style.triggers

>

style

>

resourcedictionary

>

WPF隨手小記之二 改變DataGrid樣式

大部分時候我們並不需要手動改變datagrid的樣式,因為用預設的其實也看得過去,而且在datagrid中的資料也遠比外觀重要。但總有時候,我們需要做點必要的ui修改來使datagrid更美觀。一般大家都會想到改background屬性的值,但是改完你會發現,無論你怎麼改,datagrid的外觀就是...

wpf將表中資料顯示到datagrid示例

a.在.xaml檔案中拖入乙個datagrid,然後新增列名,使用binding 如下 複製 如下 n程式設計客棧bsp b.首先把要顯示的資料查詢後放入datatable中 複製 如下 public datatable show catch exception ex return dt 注意 該方...

WPF中DataGrid控制項

設定不可自動拉伸寬度 datagrid.canuserresizecolumns false 第一列不可見 datagrid.headersvisibility datagridheadersvisibility.column 設定不可自動拉伸高度 datagrid.canuserresizerow...