我遇到这样的情况:
我有一个框架在一个窗口。我有一个页面,将加载在一个框架从代码。我有一个网格的页面,有行和列的定义从代码。
我想用按钮填充网格行和列(从矩形创建)。我写了下面的代码:
应用于每个按钮的样式:
<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="Attemptsimage.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 CLASS CODE:
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);
}
}
从MAIN调用CLASS:
公共局部类MainWindow: Window{
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的校长!
Style
是单个实例,如果您更改Style
Image
,它将更改使用Style
的所有地方。
我能想到的不创建自定义控件的最简单的解决方案是使用Buttons
Tag
属性来传递图像文件名,Tag
是DependancyProperty
,所以它支持DataBinding
。
的例子:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="rect" DataContext="{TemplateBinding Tag}" Style="{DynamicResource rectangle_style}" Cursor="Hand">
<Rectangle.Fill>
<ImageBrush ImageSource="{Binding}" 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>
</Style>
代码:
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);
btn.Tag = "The filename for this buttons image";
}