简体   繁体   中英

How can I write this XAML programmatically?

I am using materialDesign in my current WPF poject. The code is easy in XAML:

    <Button HorizontalAlignment="Right" Margin="0,0,0,0" Width="60" Height="40" Background="#FFB1CB7C" BorderBrush="#FF779151">
    <materialDesign:PackIcon Kind="TagPlus" HorizontalAlignment="Center" VerticalAlignment="Center" Width="35" Height="35"/>
</Button>

But I have to generate "x" buttons, and I cant figure out how can I do it in C# programmatically.

The Button part is easy:

        Button tag = new Button();
        tag.HorizontalAlignment = HorizontalAlignment.Right;
        tag.Width = 60;
        tag.Height = 40;
        tag.Background = new SolidColorBrush(Color.FromRgb(177, 203, 124));
        tag.BorderBrush = new SolidColorBrush(Color.FromRgb(119, 145, 81));
        tag.Margin = new Thickness(0, 0, 0, 0);

But I dont know how to do the MaterialDesign part.

The MaterialDesign part should be just like the button part.
PackIcon is just a class inside whatever namespace is pointed to by materialDesign .

As I don't know the exact classes you use, here is what I guess the code could look like:

using The.MaterialDesign.Namespace;

...

PackIcon icon = new PackIcon();
icon.Kind = IconType.TagPlus; // Replace IconType by its correct type
icon.HorizontalAlignment = HorizontalAlignment.Center;
icon.VerticalAlignment = VerticalAlignment.Center;
icon.Width = 35;
icon.Height = 35;

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