我有这种情况:从代码C#编辑样式WPF#
我在窗口中有一个fram。 我有一个页面,将加载在框架中的代码。 我在页面中有一个网格,它具有代码中的行和列定义。
我想populate网格行和列与按钮(从矩形创建)。 我已经写代码:
样式应用到每一个按钮:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="rect" Style="{DynamicResource rectangle_style}" Cursor="Hand">
<Rectangle.Fill>
<ImageBrush ImageSource="Attempts\image.jpg" Stretch="UniformToFill"/>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content=""/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True"/>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
POPOLATE类代码:
public void popolate(Grid grd)
{
for(int j = 1; j < 4; ++j)
{
for (int i = 1; i < 6; ++i)
{
Button btn = new Button();
btn.Margin= new Thickness(4,4,4,4);
grd.Children.Add(btn);
btn.SetValue(Grid.RowProperty, j);
btn.SetValue(Grid.ColumnProperty, i);
}
}
CALL类从MAIN:
公众部分类MainWindow:窗口 {
Page1 page = new Page1();
Costruct costruttore = new Costruct();
public MainWindow()
{
this.InitializeComponent();
costruttore.popolate(page.LayoutRoot);
frame_first.NavigationService.Navigate(page);
// Insert code required on object creation below this point.
}
}
我想可以设置不同的图像()是一个矩形为从后面码的每个按钮。 我如何编写这个必要的代码?
请解释一下,因为我是WPF的一个主要人物!
costruct是一个类的名字! – Loris91 2013-03-20 17:49:49
你是否需要按钮上的文字或图像? – 2013-03-20 20:47:22
只有图像,也许也是文字,但主要只有图像! 你可以给一个解决方案吗? – Loris91 2013-03-20 20:56:08