Home > Enterprise >  Uno Platform C# Create Material Card
Uno Platform C# Create Material Card

Time:12-04

I am using the Uno Platform and trying to create a Material Card through C#. I have been able to find a number of examples of a Card created in XAML but nothing in C#.

My current code is below to create a Card and add it to a Grid. I know I need to do something with adding a HeaderContentTemplate and SubHeaderContentTemplate but I am sure exactly what needs to be done.

Card myCard = new Card();
myCard.HeaderContent = "test";
myCard.SubHeaderContent = "test";
Grid.SetRow(myCard, k);
Grid.SetColumn(myCard, l);
myGrid.Children.Add(myCard);

Any help would be greatly appreciated. Thank you.

CodePudding user response:

To add a HeaderContentTemplate and SubHeaderContentTemplate to your card, you can try using the Card.HeaderContentTemplate and Card.SubHeaderContentTemplate properties. These properties allow you to specify a DataTemplate that defines the appearance of the header and subheader content of your card.

Here's an example of how you might use these properties to create a DataTemplate for your card's header and subheader:

Card myCard = new Card();

myCard.HeaderContent = "test";
myCard.SubHeaderContent = "test";

// Create a DataTemplate for the header content of the card
DataTemplate headerTemplate = new DataTemplate();
FrameworkElementFactory headerFactory = new FrameworkElementFactory(typeof(TextBlock));
headerFactory.SetValue(TextBlock.TextProperty, "Header: ");
headerFactory.SetValue(TextBlock.FontSizeProperty, 14.0);
headerFactory.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
headerTemplate.VisualTree = headerFactory;
myCard.HeaderContentTemplate = headerTemplate;

// Create a DataTemplate for the subheader content of the card
DataTemplate subheaderTemplate = new DataTemplate();
FrameworkElementFactory subheaderFactory = new FrameworkElementFactory(typeof(TextBlock));
subheaderFactory.SetValue(TextBlock.TextProperty, "Subheader: ");
subheaderFactory.SetValue(TextBlock.FontSizeProperty, 12.0);
subheaderFactory.SetValue(TextBlock.FontWeightProperty, FontWeights.Normal);
subheaderTemplate.VisualTree = subheaderFactory;
myCard.SubHeaderContentTemplate = subheaderTemplate;

Grid.SetRow(myCard, k);
Grid.SetColumn(myCard, l);
myGrid.Children.Add(myCard);

In this example, we create a DataTemplate for each of the card's header and subheader content. The DataTemplate specifies the appearance of the content by using a TextBlock element. We then set the Card.HeaderContentTemplate and Card.SubHeaderContentTemplate properties to the DataTemplate objects we created.

CodePudding user response:

I was not able to get the Material Cards working through C# so I ended up making a template xaml and C# file which I build the structure of my card. Then I created that through C#.

  • Related