1. 程式人生 > 其它 >WPF的DataGrid繫結資料轉換

WPF的DataGrid繫結資料轉換

最近在使用WPF的時候,遇到某個列的值需要根據內容不同進行轉換顯示的需求。搜尋整理了一下,分以下兩種方式實現:

1、轉換器方法(Converter),繫結後,觸發轉換器,轉換器負責把值轉換成需要的內容。

2、DataTrigger方法,直接在xaml裡面對資料進行處理,展示所需要的內容。

這裡對這兩種方法做下簡單的總結,方便自己以後檢視,也方便有需要的人。

第一種:

1、定義轉換器

 1 [ValueConversion(typeof(int), typeof(string))]
 2     public class DataConverter : IValueConverter
 3     {
4 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 5 { 6 int c = System.Convert.ToInt32(parameter); 7 8 if (value == null) 9 throw new ArgumentNullException("value can not be null"); 10 11 int
index = System.Convert.ToInt32(value); 12 if (index == 1) 13 return "Blue"; 14 else if (index == 2) 15 return "Red"; 16 else 17 return "Green"; 18 } 19 20 public object ConvertBack(object value, Type targetType, object
parameter, CultureInfo culture) 21 { 22 return null; 23 } 24 }

2、使用轉換器

(1)引用名稱空間

xmlns:local="clr-namespace:觸發器測試"
View Code

(2)定義資源

1 <Window.Resources>
2         <local:DataConverter x:Key="foreColor"></local:DataConverter>
3     </Window.Resources>
View Code

(3)繫結屬性,新增轉換器

1 <Grid>
2         <DataGrid x:Name="dataGrid1"  AutoGenerateColumns="False">
3             <DataGrid.Columns>
4                 <DataGridTextColumn Binding="{Binding ID}" Header="序號" Width="*"/>
5                 <DataGridTextColumn Binding="{Binding Name,Converter={StaticResource foreColor}}" Header="名稱" Width="*"/>
6             </DataGrid.Columns>
7         </DataGrid>
8     </Grid>
View Code

第二種:

 1  <DataGrid x:Name="dataGrid1"  AutoGenerateColumns="False">
 2             <DataGrid.Columns>
 3                 <DataGridTextColumn Binding="{Binding ID}" Header="序號" Width="*"/>
 4                 <DataGridTemplateColumn  Header="名稱" Width="*" >
 5                     <DataGridTemplateColumn.CellTemplate>
 6                         <DataTemplate>
 7                             <TextBlock>
 8                                 <TextBlock.Style>
 9                                     <Style TargetType="TextBlock" >
10                                         <Style.Triggers>
11                                             <DataTrigger Binding="{Binding Path= Name}" Value="1">
12                                                 <Setter Property="Text"  Value=""></Setter>
13                                             </DataTrigger>
14                                             <DataTrigger Binding="{Binding Path= Name}" Value="2">
15                                                 <Setter Property="Text"  Value=""></Setter>
16                                             </DataTrigger>
17                                         </Style.Triggers>
18                                     </Style>
19                                 </TextBlock.Style>
20                             </TextBlock>
21                         </DataTemplate>
22                     </DataGridTemplateColumn.CellTemplate>
23                 </DataGridTemplateColumn>
24             </DataGrid.Columns>
25         </DataGrid>
 <物件.Style>
                <Style TargetType="物件">              
                    <Style.Triggers>
                        <DataTrigger Binding="{繫結}" Value="">
                          //寫你想要的效果
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
</物件.Style>
核心程式碼模板

初識WPF,簡單記錄,虛心求教