请问能在Silverlight项目的CS文件中创建ControlTemplate吗?
在Silverlight项目中,想把grid 套在ControlTemplate 中,请问能这样实例化ControlTemplate吗?或者还是根本不能?怎么实例化呢?
下面的代码定义好了模板内的内容,但不知道怎么把grid加入模板?
ControlTemplate ct = new ControlTemplate();
////初始化变量
RowDefinition rdf = null;
ColumnDefinition cdf = null;
SolidColorBrush pSolidColorBrush = null;
Color pColor = Colors.Red;
TextBlock tb = null;
/////初始化grid
Grid gd = new Grid();
gd.Width = 200;
gd.Height = 300;
gd.ShowGridLines = false;
gd.HorizontalAlignment = HorizontalAlignment.Center;
gd.VerticalAlignment = VerticalAlignment.Center;
pSolidColorBrush = new SolidColorBrush();
pColor = Colors.White;
pSolidColorBrush.Color = pColor;
gd.Background = pSolidColorBrush;
/////定义行1
rdf = new RowDefinition();
rdf.Height = new GridLength(160, GridUnitType.Pixel);
gd.RowDefinitions.Add(rdf);
/////定义行2
rdf = new RowDefinition();
rdf.Height = new GridLength(30, GridUnitType.Pixel);
gd.RowDefinitions.Add(rdf);
/////定义行3
rdf = new RowDefinition();
rdf.Height = new GridLength(30, GridUnitType.Pixel);
gd.RowDefinitions.Add(rdf);
/////定义行4
rdf = new RowDefinition();
rdf.Height = new GridLength(30, GridUnitType.Pixel);
gd.RowDefinitions.Add(rdf);
/////定义行5
rdf = new RowDefinition();
rdf.Height = new GridLength(30, GridUnitType.Pixel);
gd.RowDefinitions.Add(rdf);
/////定义行6
rdf = new RowDefinition();
rdf.Height = new GridLength(30, GridUnitType.Pixel);
gd.RowDefinitions.Add(rdf);
/////定义列
cdf = new ColumnDefinition();
cdf.Width = new GridLength(70, GridUnitType.Pixel);
gd.ColumnDefinitions.Add(cdf);
/////定义列
cdf = new ColumnDefinition();
cdf.Width = new GridLength(130, GridUnitType.Pixel);
gd.ColumnDefinitions.Add(cdf);
/////初始化image
Image img = new Image();
BitmapImage bitImage = new BitmapImage();
byte[] value = p.Attributes["pic_byte"] as byte[];
if (value != null)
{
byte[] ppic = value as byte[];
MemoryStream ms = new MemoryStream(ppic);
bitImage.SetSource(ms);
img.Source = bitImage;
}
Grid.SetRow(img, 0);
Grid.SetColumn(img, 0);
Grid.SetColumnSpan(img, 2);
Grid.SetRowSpan(img, 1);
gd.Children.Add(img);
tb = new TextBlock();
tb.Text = "详细地址";
tb.HorizontalAlignment = HorizontalAlignment.Center;
tb.VerticalAlignment = VerticalAlignment.Center;
Grid.SetRow(tb, 0);
Grid.SetColumn(tb, 0);
Grid.SetColumnSpan(tb, 1);
Grid.SetRowSpan(tb, 1);
gd.Children.Add(tb);
tb = new TextBlock();
tb.Text = p.Attributes["address"].ToString();
tb.HorizontalAlignment = HorizontalAlignment.Center;
tb.VerticalAlignment = VerticalAlignment.Center;
Grid.SetRow(tb, 0);
Grid.SetColumn(tb, 1);
Grid.SetColumnSpan(tb, 1);
Grid.SetRowSpan(tb, 1);
gd.Children.Add(tb);
[解决办法]
建议在XAML里写ControlTemplate,在CS里写业务代码:
比如,你想写个自己的CustomerButton,里面有图片。当click图片时,你可以抛出一个事件。
在xaml里面:
<Style TargetType="local:CustomButton"> <Setter Property="Cursor" Value="Hand"/> <Setter Property="Margin" Value="10"/> <Setter Property="Effect"> <Setter.Value> <DropShadowEffect ShadowDepth="5" Color="Gray"/> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:CustomButton"> <Border CornerRadius="6" x:Name="border" BorderThickness="1"> <Border.Background> <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#4C8CC8" Offset="0"/> <GradientStop Color="#3271AF" Offset="0.5"/> <GradientStop Color="#0B4A89" Offset="1"/> </LinearGradientBrush> </Border.Background> <Grid x:Name="Root" Height="{TemplateBinding ButtonHeight}" Width="{TemplateBinding ButtonWidth}"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Image x:Name="loadingImage" Source="{TemplateBinding ButtonImage}" Cursor="Hand" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5" Stretch="Uniform" Grid.Row="0" /> <TextBlock x:Name="content" Text="{TemplateBinding Text}" Foreground="White" Grid.Row="1" FontSize="14" FontFamily="Arial" HorizontalAlignment="Center" Margin="5" TextWrapping="Wrap" VerticalAlignment="Bottom"/> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>