简体   繁体   中英

Edit style WPF from code C#

i've this situation:

i've a fram in a windows. i've a page that will be load in a frame from code. i've a grid in a page that have row and column definition from code.

I want to popolate the grid row and columns with buttons (created from rectangle). I've write this code:

STYLE APPLIED TO EVERY BUTTONS:

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


            }



        }

CALL CLASS FROM MAIN:

public partial 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.
    }

}

i want can set a different image ()in a rectangle for every button from behind code. how i can write code for this necessity??

Please explain me well because i'm a principiant of WPF!

The Style is a single instance, if you change the Style Image it will change everywhere that is using the Style .

The simplest solution I can think of without creating a custom control would be to use the Buttons Tag property to pass in the image filename, Tag is a DependancyProperty so it supports DataBinding .

Example:

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

Code:

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM