supercell
supercell

Reputation: 309

XAML Code in C#

I've been trying to convert the following XAML code to C#, but without success. I'm kinda stuck at the ControlTemplate part. I'm very new at WPF, so I might be doing this all wrong.

The idea behind this is that I have to retrieve data from a database and wrap each record with the below template. Since the rows may vary in amount I want to generate each template using C# code.

I purposely didn't define all the control attributes yet.

             <Grid>
                <Button Content="Button" Height="30" Name="button1" Margin="8,8,7,8" Click="button1_Click" >
                    <Button.Template>
                        <ControlTemplate>
                            <Rectangle RadiusX="5" RadiusY="5" Stroke="LightYellow" StrokeThickness="0.5" Name="myRectangle">
                                <Rectangle.Fill>
                                    <VisualBrush Opacity="0.7">
                                        <VisualBrush.Visual>
                                            <TextBlock Name="myTextBlock" Foreground="LightYellow" Background="DarkBlue" Text="Text here"  />
                                        </VisualBrush.Visual>
                                    </VisualBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </Grid>

My C# code so far:

Grid grid = new Grid();
        Button button = new Button();
        ControlTemplate controlTemplate = new ControlTemplate();
        Rectangle rectangle = new Rectangle();
        VisualBrush visualBrush = new VisualBrush();
        TextBlock textBlock = new TextBlock();
        textBlock.Text = "Text here";
        textBlock.Background = new SolidColorBrush(Color.FromRgb(2, 33, 233));

        visualBrush.Visual = textBlock;
        visualBrush.Opacity = 0.7;
        rectangle.Fill = visualBrush;

        controlTemplate //What to do here?

        button.Template = controlTemplate;
        button.Content = "Button";
        button.Height = 30;
        button.Width = 100;
        grid.Children.Add(button);
        this.Content = grid;

Thanks in advance,

Grant

Upvotes: 2

Views: 1667

Answers (1)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84647

This code behind should be equivalent to your xaml.
When "converting" from Xaml to code behind it's almost easier to work inside out, starting with TextBlock in this case, and then work your way up.

TextBlock textBlock = new TextBlock();
textBlock.Name = "myTextBlock";
textBlock.Foreground = Brushes.LightYellow;
textBlock.Background = Brushes.DarkBlue;
textBlock.Text = "Text here";

VisualBrush visualBrush = new VisualBrush();
visualBrush.Opacity = 0.7;
visualBrush.Visual = textBlock;

FrameworkElementFactory rectangle = new FrameworkElementFactory(typeof(Rectangle));
rectangle.SetValue(Rectangle.RadiusXProperty, 5.0);
rectangle.SetValue(Rectangle.RadiusYProperty, 5.0);
rectangle.SetValue(Rectangle.StrokeProperty, Brushes.LightYellow);
rectangle.SetValue(Rectangle.StrokeThicknessProperty, 0.5);
rectangle.SetValue(Rectangle.NameProperty, "myRectangle");
rectangle.SetValue(Rectangle.FillProperty, visualBrush);

ControlTemplate controlTemplate = new ControlTemplate();
controlTemplate.VisualTree = rectangle;

Button button = new Button();
button.Content = "Button";
button.Height = 30;
button.Name = "button1";
button.Margin = new Thickness(8, 8, 7, 8);
button.Click += new RoutedEventHandler(button_Click);
button.Template = controlTemplate;

Grid grid = new Grid();
grid.Children.Add(button);

Upvotes: 2

Related Questions