轉帖|其它|編輯:郝浩|2011-02-28 14:26:21.000|閱讀 2713 次
概述:動態綁定Silverlight的DataGrid,比如點一下按鈕刷新的時候,重新綁定,當時是沒反應的,等你點第二下才反應過來。這可能是數據源不是雙向綁定的吧,看下面解釋:要對DataGrid進行數據綁定,通常我們需要定義具體類,而后建立類的范式集合。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
動態綁定Silverlight的DataGrid,比如點一下按鈕刷新的時候,重新綁定,當時是沒反應的,等你點第二下才反應過來。這可能是數據源不是雙向綁定的吧,看下面解釋:要對DataGrid進行數據綁定,通常我們需要定義具體類,而后建立類的范式集合。如下所示:
public class BasicInfo
{
public string id { set; get; }
public string name { set; get; }
public string tel { set; get; }
}
private void LoadDataByClass()
{
var result = new ObservableCollection<BasicInfo>();
result.Add(new BasicInfo { id = "1001", name = "Ivan", tel = "135462512" });
result.Add(new BasicInfo { id = "1002", name = "Ivan2", tel = "135462512" });
result.Add(new BasicInfo { id = "1003", name = "Ivan3", tel = "135462512" });
dgshow.ItemsSource = result;
}
也就說說,如果要在DataGrid中show集合數據,就需要有具體類的存在【匿名類在綁定的時候會出現運行期異常】。這在很多情況下是可行的.
但是有時候,某個集合只需要show一次而已,而我們又不得不給建立具體類以使得其信息能夠在DataGrid中顯示。也就是說在要顯示的集合的列不確定時,建立具體類實在是有點“浪費”。
這種情況下,我們可以建立“自己”的集合類【這時候免不了要用到Indexer】,利用Dictionary來動態建立集合列,從而實現數據的動態添加和顯示:
public class Row
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();
public object this[string index]
{
get
{
return dictionary[index];
}
set
{
dictionary[index] = value;
}
}
}
private void LoadDataByDictionary()
{
var result = new ObservableCollection<Row>();
Row row = new Row();
row["id"] = "Dic1001";
row["name"] = "Ivan";
row["tel"] = "123456";
result.Add(row);
row = new Row();
row["id"] = "Dic1002";
row["name"] = "Ivan2";
row["tel"] = "12345622";
result.Add(row);
dgshowDic.ItemsSource = result;
}
這樣產生的集合,在DataGrid綁定時,綁定的為Row.所以在UI顯示之前,要對數據集合進行轉換,使得DataGrid中顯示需要顯示的列數據:
public class RowIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Row row = value as Row;
string index = parameter as string;
return row[index];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
附帶Xaml的定義代碼:
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="CodeProjectDemo.Page"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CodeProjectDemo"
Loaded="UserControl_Loaded" >
<UserControl.Resources>
<local:RowIndexConverter x:Key="RowIndexConverter"></local:RowIndexConverter>
<local:DateConverter x:Key="DateConverter"></local:DateConverter>
</UserControl.Resources>
<Canvas>
<Button x:Name="btnDic" Height="50" Width="150" Content="LoadDataBy Dictionary" FontSize="12" Click="btnDic_Click"></Button>
<Button x:Name="btnCls" Canvas.Left="200" Height="50" Width="150" Content="LoadDataBy Class" FontSize="12" Click="btnCls_Click" ></Button>
<data:DataGrid x:Name="dgshow" Canvas.Top="70" Canvas.Left="400" Height="auto" Width="150" AutoGenerateColumns="True"></data:DataGrid>
<data:DataGrid Name="dgshowDic" Canvas.Top="70" AutoGenerateColumns="False" Height="auto" IsReadOnly="False">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="id" Binding="{Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=id}"/>
<data:DataGridTextColumn Header="name" Binding="{Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=name}"/>
<data:DataGridTextColumn Header="tel" Binding="{Binding Converter={StaticResource RowIndexConverter}, ConverterParameter=tel}"/>
</data:DataGrid.Columns>
</data:DataGrid>
<ListBox x:Name="MyListBox" Canvas.Top="300" Canvas.Left="10" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Time1, Converter={StaticResource DateConverter}}" Margin="5" Foreground="Red"></TextBlock>
<TextBlock Text="{Binding Time2}" Margin="5"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Canvas>
</UserControl>
ps:注意到 IValueConverter 的應用,可以參考msdn.
建立轉換類后,其將作為一個資源存在,故而在xaml中要對此資源進行說明【見上述xaml】。
下面是對此接口的一個簡單應用:在ListBox中顯示時間前對其進行格式化。
public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
return date.ToShortDateString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class MyTime
{
public DateTime Time1 { get; set; }
public DateTime Time2 { get; set; }
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載