8,734
社区成员
发帖
与我相关
我的任务
分享
<Style x:Key="NameStyle" TargetType="telerik:GridViewHeaderCell">
<Setter Property="FontSize" Value="13"/>
<Setter Property="Background" Value="AliceBlue"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid x:Name="Root">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="0">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<TextBlock Text="显示文本" Foreground="#010101"></TextBlock>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
<Rectangle Fill="#C0CBD9" VerticalAlignment="Stretch" Height="1" Visibility="Visible" Grid.Row="1" />
<ContentPresenter Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Image Source="/Buttons/Show.png"></Image>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
System.Windows.Style btnStyle = new System.Windows.Style();
//btnStyle.TargetType = typeof(System.Windows.Controls.Control);
btnStyle.TargetType = typeof(GridViewHeaderCell);
ControlTemplate template = new ControlTemplate();
Grid grid = new Grid();
RowDefinition rd = new RowDefinition();
GridLength gl = new GridLength(1);
rd.Height = gl;
grid.RowDefinitions.Add(rd);
TextBlock bl = new TextBlock();
bl.Name = "bl";
bl.Text = "2";
bl.SetValue(Grid.RowProperty, 0);
grid.Children.Add(bl);
Setter temp = new Setter(System.Windows.Controls.Control.TemplateProperty, template);
btnStyle.Setters.Add(temp);
FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory row1 = new FrameworkElementFactory(typeof(RowDefinition));
row1.SetValue(HeightProperty, 20.0);
FrameworkElementFactory row2 = new FrameworkElementFactory(typeof(RowDefinition));
row2.SetValue(HeightProperty, 20.0);
gridFactory.AppendChild(row1);
gridFactory.AppendChild(row2);
FrameworkElementFactory textBlockFactory1 = new FrameworkElementFactory(typeof(TextBlock));
textBlockFactory1.SetValue(TextBlock.TextProperty, "显示文本1");
textBlockFactory1.SetValue(ForegroundProperty, new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString("#010101")));
textBlockFactory1.SetValue(Grid.RowProperty, 0); // 设置Grid.Row
gridFactory.AppendChild(textBlockFactory1);
FrameworkElementFactory textBlockFactory2 = new FrameworkElementFactory(typeof(TextBlock));
textBlockFactory2.SetValue(TextBlock.TextProperty, "显示文本2");
textBlockFactory2.SetValue(ForegroundProperty, new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString("#010101")));
textBlockFactory2.SetValue(Grid.RowProperty, 1);
gridFactory.AppendChild(textBlockFactory2);
Style style = new Style();
style.TargetType = typeof(Button);
Setter setter = new Setter();
setter.Property = ContentControl.TemplateProperty;
ControlTemplate temp = new ControlTemplate(typeof(Button));
temp.VisualTree = gridFactory;
setter.Value = temp;
style.Setters.Add(setter);
btn1.Style = style;
这样就定义个一个button的ControlTemplate。其它属性 通过添加Setter来设定。