如何对Silverlight DataGrid进行动态数据绑定
1个回答
展开全部
动态绑定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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://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; }
}
源不是双向绑定的吧,看下面解释:要对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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://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; }
}
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询