Creating elements dynamically
-
Friday, August 17, 2012 5:14 PM
Hello.
I'm trying to create a button dynamically in a grid. The button appears but the features no. This is my code:
Button newButton = new Button();
newButton.Height = 75;
newButton.Width = 250;
newButton.Content = "Dynamically added";
newButton.Margin = new Thickness(302, 223, 0, 0);
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/Logo.png"));
newButton.Background = brush1;
itemGridView.Items.Add(newButton);Thanks!
All Replies
-
Friday, August 17, 2012 5:20 PMyou mean the image?
-
Friday, August 17, 2012 8:05 PM
The button appears. But do not get the properties I put There is no picture too:
newButton.Height = 75;
newButton.Width = 250;
newButton.Content = "Dynamically added";
newButton.Margin = new Thickness(302, 223, 0, 0);
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/Logo.png"));
newButton.Background = brush1; -
Saturday, August 18, 2012 8:29 PM
<Page x:Class="App1.MainPage" IsTabStop="false" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <GridView x:Name="gridView" Grid.Row="0" Grid.Column="0" SelectionMode="Multiple"> <GridView.Items> <Button Content="First"/> </GridView.Items> <GridView.ItemContainerStyle> <Style TargetType="GridViewItem"> <Setter Property="Width" Value="800"/> <Setter Property="Height" Value="300"/> </Style> </GridView.ItemContainerStyle> </GridView> <Button Grid.Row="1" Content="Add" Click="Button_Click_2"/> </Grid> </Page>---
using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 namespace App1 { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { } private void Button_Click_2(object sender, RoutedEventArgs e) { Button newButton = new Button(); newButton.Height = 75; newButton.Width = 250; newButton.Content = "Dynamically added"; newButton.Margin = new Thickness(30, 20, 0, 0); // !!! Set Margins not to exceed ItemContainerStyle Width & Height ImageBrush brush1 = new ImageBrush(); brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/Logo.png")); newButton.Background = brush1; // ... gridView.Items.Add(newButton); } } }
- Edited by ForInfoMicrosoft Community Contributor Saturday, August 18, 2012 8:35 PM
- Proposed As Answer by Dave SmitsMicrosoft Community Contributor Saturday, August 18, 2012 8:36 PM
- Marked As Answer by Vicky SongModerator Friday, August 31, 2012 7:23 AM


