silverlight3 datagrid c#中动态生成模板列(日期格式)使用IValueConvert对绑定数据的格式化操作
生成datagrid模板列代码 用c#(格式转换)
void GetDateTimeTemplate(DataGrid dgrid, string headername, string bindingname)
{
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = headername;
StringBuilder CellTemp = new StringBuilder();
CellTemp.Append("<DataTemplate ");
CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
CellTemp.Append("2006/xaml/presentation' ");
CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
//Be sure to replace "YourNamespace" and "YourAssembly" with your app's
//actual namespace and assembly here
CellTemp.Append("xmlns:local='clr-namespace:AlantusBI");
CellTemp.Append(";assembly=AlantusBI'>");
CellTemp.Append("<Grid>");
CellTemp.Append("<Grid.Resources>");
CellTemp.Append("<local:DateTimeConverter x:Key='DateConverter' />");
CellTemp.Append("</Grid.Resources>");
CellTemp.Append("<TextBlock ");
CellTemp.Append("Text = '{Binding " + bindingname + ", ");
CellTemp.Append("Converter={StaticResource DateConverter}}' ");
CellTemp.Append("Margin='4'/>");
CellTemp.Append("</Grid>");
CellTemp.Append("</DataTemplate>");
StringBuilder CellETemp = new StringBuilder();
CellETemp.Append("<DataTemplate ");
CellETemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
CellETemp.Append("2006/xaml/presentation' ");
CellETemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
CellETemp.Append("xmlns:basics='clr-namespace:System.Windows.Controls;");
CellETemp.Append("assembly=System.Windows.Controls' >");
CellETemp.Append("<basics:DatePicker ");
CellETemp.Append("SelectedDate='{Binding " + bindingname + ", Mode=TwoWay}' />");
CellETemp.Append("</DataTemplate>");
templateColumn.CellTemplate = (DataTemplate)XamlReader.Load(CellTemp.ToString());
templateColumn.CellEditingTemplate = (DataTemplate)XamlReader.Load(CellETemp.ToString());
dgrid.Columns.Add(templateColumn);
#region 数字转换 123,345.00
/// <summary>
/// 数字格式转换 如123,345.00
/// </summary>
public class DoubleConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
//Double d = (double )value;
//return d.ToString("N2");
//上面这样写如果传入的值是string类型 不能转换
try
{
Double d = double.Parse(value.ToString());
return d.ToString("N2");
}
catch (Exception ex)
{
}
return value;
}
public object ConvertBack(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
string strValue = value.ToString();
Double result;
if (Double.TryParse(strValue, out result))
{
return result;
}
return value;
}
}
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightAppTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:SilverlightAppTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<UserControl.Resources>
<test:DoubleConverter x:Key="DoubleConverter"></test:DoubleConverter>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<data:DataGrid x:Name="dg">
<data:DataGrid.Columns> <!--Converter={StaticResource DoubleConverter},-->
<data:DataGridTextColumn Header="Tax ID" Binding="{Binding TaxID, Mode=OneWay}" />
<data:DataGridTextColumn Header="价格" Binding="{Binding Price,Converter={StaticResource DoubleConverter},Mode=OneWay}" />
<data:DataGridTemplateColumn Header="Customer Since">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Price}" x:Name="txtPrice"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Customer Since">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="AAA"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
<TextBlock Text="BBB"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
<TextBlock Text="CCC"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
<TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
<data:DataGrid x:Name="datagrid" HeadersVisibility="None">
</data:DataGrid>
</StackPanel>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
<!--<data:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel x:Name="SignerPanel" Margin="48,0,0,0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding BorrowerName, Mode=OneWay}" />
<TextBlock Text=": Signers" />
</StackPanel>
<data:DataGrid x:Name="SignerSubGrid" IsReadOnly="False" AutoGenerateColumns="False"
Visibility="{Binding Path=Signer, Converter={StaticResource VisibilityConverter}}"
RowEditEnded="SignerSubGrid_RowEditEnded">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="Signer Type">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="SignerTypeCbo"
SelectedItem="{Binding Path=SignerType, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource SignerTypes}}"
Height="23" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTextColumn Header="Required Signer" Binding="{Binding RequiredSigner, Mode=TwoWay}" />
<data:DataGridTextColumn Header="Business Affiliation" Binding="{Binding BusinessAffiliation, Mode=TwoWay}" />
<data:DataGridTextColumn Header="Type of Business" Binding="{Binding TypeOfBusiness, Mode=TwoWay}" />
</data:DataGrid.Columns>
</data:DataGrid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,12,0,12">
<Button x:Name="addNewSigner" Content="Add Signer"
Height="23" Margin="4,0,4,0"
Click="addNewSigner_Click" />
</StackPanel>
</StackPanel>
</DataTemplate>
</data:DataGrid.RowDetailsTemplate>-->
</data:DataGrid>
<ScrollViewer Grid.Row="1">
<Canvas x:Name="MainCanvas" Background="White">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" >
<Image x:Name="img" Stretch="None" Margin="16" Source="china_life_logo.png" />
<Image x:Name="img2" Stretch="Fill" Margin="16"/>
<!--<Image x:Name="img3" Stretch="None" Margin="16" Source="main_visual_dim.jpg"/>-->
</StackPanel>
</Canvas>
</ScrollViewer>
<Button x:Name="btn" Width="100" Height="25" Grid.Row="2" Click="btn_Click"></Button>
<Button x:Name="btnExport" Content="导出到Excel" Width="100" Height="25" Grid.Row="3" Click="btnExport_Click"></Button>
</Grid>
</UserControl>
编辑Page.xaml的后台代码,Page.xaml.cs的代码为
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
List<Book> list = new List<Book>();
for (int i = 0; i < 4; i++)
{
Book book = new Book();
book.ID = i.ToString();
book.Name = i.ToString() + "Silverlight开发 kafa";
book.Price = 123456;
list.Add(book);
}
}
主要是在页面中修改代码 添加上资源
按F5运行可看到价格段的记录在datagrid中显示时具有格式(本例中显示为 123,345.00)
void GetDateTimeTemplate(DataGrid dgrid, string headername, string bindingname)
{
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = headername;
StringBuilder CellTemp = new StringBuilder();
CellTemp.Append("<DataTemplate ");
CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
CellTemp.Append("2006/xaml/presentation' ");
CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
//Be sure to replace "YourNamespace" and "YourAssembly" with your app's
//actual namespace and assembly here
CellTemp.Append("xmlns:local='clr-namespace:AlantusBI");
CellTemp.Append(";assembly=AlantusBI'>");
CellTemp.Append("<Grid>");
CellTemp.Append("<Grid.Resources>");
CellTemp.Append("<local:DateTimeConverter x:Key='DateConverter' />");
CellTemp.Append("</Grid.Resources>");
CellTemp.Append("<TextBlock ");
CellTemp.Append("Text = '{Binding " + bindingname + ", ");
CellTemp.Append("Converter={StaticResource DateConverter}}' ");
CellTemp.Append("Margin='4'/>");
CellTemp.Append("</Grid>");
CellTemp.Append("</DataTemplate>");
StringBuilder CellETemp = new StringBuilder();
CellETemp.Append("<DataTemplate ");
CellETemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
CellETemp.Append("2006/xaml/presentation' ");
CellETemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
CellETemp.Append("xmlns:basics='clr-namespace:System.Windows.Controls;");
CellETemp.Append("assembly=System.Windows.Controls' >");
CellETemp.Append("<basics:DatePicker ");
CellETemp.Append("SelectedDate='{Binding " + bindingname + ", Mode=TwoWay}' />");
CellETemp.Append("</DataTemplate>");
templateColumn.CellTemplate = (DataTemplate)XamlReader.Load(CellTemp.ToString());
templateColumn.CellEditingTemplate = (DataTemplate)XamlReader.Load(CellETemp.ToString());
dgrid.Columns.Add(templateColumn);
日期格式转换类 数字格式转换类
Code
#region 数字转换 123,345.00
/// <summary>
/// 数字格式转换 如123,345.00
/// </summary>
public class DoubleConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
//Double d = (double )value;
//return d.ToString("N2");
//上面这样写如果传入的值是string类型 不能转换
try
{
Double d = double.Parse(value.ToString());
return d.ToString("N2");
}
catch (Exception ex)
{
}
return value;
}
public object ConvertBack(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
string strValue = value.ToString();
Double result;
if (Double.TryParse(strValue, out result))
{
return result;
}
return value;
}
}
在Silverlight中我们可以使用IValueConverter 实现绑定数据的格式化 在页面中使用
Page.xaml界面设计,Page.xaml代码如下:
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightAppTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:SilverlightAppTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<UserControl.Resources>
<test:DoubleConverter x:Key="DoubleConverter"></test:DoubleConverter>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<data:DataGrid x:Name="dg">
<data:DataGrid.Columns> <!--Converter={StaticResource DoubleConverter},-->
<data:DataGridTextColumn Header="Tax ID" Binding="{Binding TaxID, Mode=OneWay}" />
<data:DataGridTextColumn Header="价格" Binding="{Binding Price,Converter={StaticResource DoubleConverter},Mode=OneWay}" />
<data:DataGridTemplateColumn Header="Customer Since">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Price}" x:Name="txtPrice"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Customer Since">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="AAA"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
<TextBlock Text="BBB"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
<TextBlock Text="CCC"
VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
<TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0" />
<data:DataGrid x:Name="datagrid" HeadersVisibility="None">
</data:DataGrid>
</StackPanel>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
<!--<data:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel x:Name="SignerPanel" Margin="48,0,0,0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding BorrowerName, Mode=OneWay}" />
<TextBlock Text=": Signers" />
</StackPanel>
<data:DataGrid x:Name="SignerSubGrid" IsReadOnly="False" AutoGenerateColumns="False"
Visibility="{Binding Path=Signer, Converter={StaticResource VisibilityConverter}}"
RowEditEnded="SignerSubGrid_RowEditEnded">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="Signer Type">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="SignerTypeCbo"
SelectedItem="{Binding Path=SignerType, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource SignerTypes}}"
Height="23" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTextColumn Header="Required Signer" Binding="{Binding RequiredSigner, Mode=TwoWay}" />
<data:DataGridTextColumn Header="Business Affiliation" Binding="{Binding BusinessAffiliation, Mode=TwoWay}" />
<data:DataGridTextColumn Header="Type of Business" Binding="{Binding TypeOfBusiness, Mode=TwoWay}" />
</data:DataGrid.Columns>
</data:DataGrid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,12,0,12">
<Button x:Name="addNewSigner" Content="Add Signer"
Height="23" Margin="4,0,4,0"
Click="addNewSigner_Click" />
</StackPanel>
</StackPanel>
</DataTemplate>
</data:DataGrid.RowDetailsTemplate>-->
</data:DataGrid>
<ScrollViewer Grid.Row="1">
<Canvas x:Name="MainCanvas" Background="White">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" >
<Image x:Name="img" Stretch="None" Margin="16" Source="china_life_logo.png" />
<Image x:Name="img2" Stretch="Fill" Margin="16"/>
<!--<Image x:Name="img3" Stretch="None" Margin="16" Source="main_visual_dim.jpg"/>-->
</StackPanel>
</Canvas>
</ScrollViewer>
<Button x:Name="btn" Width="100" Height="25" Grid.Row="2" Click="btn_Click"></Button>
<Button x:Name="btnExport" Content="导出到Excel" Width="100" Height="25" Grid.Row="3" Click="btnExport_Click"></Button>
</Grid>
</UserControl>
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
List<Book> list = new List<Book>();
for (int i = 0; i < 4; i++)
{
Book book = new Book();
book.ID = i.ToString();
book.Name = i.ToString() + "Silverlight开发 kafa";
book.Price = 123456;
list.Add(book);
}
}
<UserControl.Resources>
<test:DoubleConverter x:Key="DoubleConverter"></test:DoubleConverter>
</UserControl.Resources>
<test:DoubleConverter x:Key="DoubleConverter"></test:DoubleConverter>
</UserControl.Resources>