1

我有这种情况:

我在 Windows 中有一个框架。我有一个将从代码加载到框架中的页面。我在页面中有一个网格,其中包含代码中的行和列定义。

我想用按钮(从矩形创建)填充网格行和列。我写了这段代码:

适用于每个按钮的样式:

<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>

流行类代码:

 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);


            }



        }

从主要呼叫课程:

公共部分类 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 的负责人!

4

1 回答 1

1

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";
}
于 2013-03-20T21:16:26.577 回答