How to put textblock in container to add background color

I am needing to put TextBlock inside a container e.g. a Grid In order to put a background color. I know how this works in xaml but in C# -> (xaml.cs) a TextBlock is made every time the SaveClose button is clicked. Therefore one is not originally created.

        //In text editing mode.
        if (Notepad.Visibility == Visibility.Visible)
        {
            TextBlock block = new TextBlock();
            block.Width = 250;
            block.Height = 100;
            block.Text = Notepad.Text;
            block.Foreground = new SolidColorBrush(Colors.Blue);

<Page.BottomAppBar>
    <AppBar>
        <StackPanel Orientation="Horizontal">
            <Button Name="SaveClose" Style="{StaticResource AppBarButtonStyle}" Content="&#x2714;" AutomationProperties.Name="Save and Close" Click="SaveClose_Click" />
            <Button Name="Delete" Style="{StaticResource AppBarButtonStyle}" Content="&#xE107;" AutomationProperties.Name="Delete Selected" Click="Delete_Click" />
        </StackPanel>    
    </AppBar>
</Page.BottomAppBar>

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" RenderTransformOrigin="0.515,0.505">
    <TextBox x:Name="Notepad" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="748" Width="1366" FontSize="30" Visibility="Collapsed"/>
    <GridView x:Name="NoteGrid" HorizontalAlignment="Left" Margin="106,350,0,0" VerticalAlignment="Top" Width="363" Height="168">
        <Button x:Name="NewNote" Content="Create New Note" Height="150" Width="348" FontSize="40" Margin="0" Click="NewNote_Click"/>
    </GridView>
    <GridView x:Name="NoteOutGrid" HorizontalAlignment="Left" Margin="682,84,0,0" VerticalAlignment="Top" Width="674" Height="590"/>

I want every new textBlock created to have the same background, how do I go about this? Thanks

Answers


In this case a picture would have helped. I am assuming you are wanting to know how to add your TextBlock to a Grid.

First of all you will need to give your grid a Name so it can be referenced in the CodeBehind. Then simply add your TextBlock to the Grids Children Collection.(I am using a Name of gridName here).

gridName.Children.Add(block);

if your Grid has rows an columns you will need to use attached properties to Assign your control to the proper location.( I am using rowId and columnId to designate the row and column number of the Grid that the control is being put.

Grid.SetColumn(block, columnId);
Grid.SetRow(block, rowId);

Putting it together:

MainWindow.Xaml.cs

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            TextBlock block = new TextBlock();
            block.Width = 250;
            block.Height = 100;
            block.Text = "Hello World"; // Notepad.Text;
            block.Foreground = new SolidColorBrush(Colors.Blue);
            gridName.Children.Add(block);
            Grid.SetColumn(block, 0);  /Not neccesary since not multiple columns
            Grid.SetRow(block, 0);
        }
    }
}

MainWindow.Xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
   <Grid Name="gridName" Background="PaleGoldenrod" >
       <Grid.RowDefinitions>
           <RowDefinition Height="4*"/>
           <RowDefinition Height="*" />
       </Grid.RowDefinitions>
       <Button Content="Button" Height="23" Grid.Row="1" HorizontalAlignment="Center"   Name="button1" VerticalAlignment="Center" Width="75" Click="button1_Click" />
   </Grid>
</Window>

Example using UserControl.

Note.xaml.cs

namespace WpfApplication1
{
   /// <summary>
   /// Interaction logic for Note.xaml
   /// </summary>
   public partial class Note : UserControl
   {
       public Note()
       {
           InitializeComponent();
       }

       public string Text
       {
           get { return block.Text; }
           set { block.Text = value; }
       }

       public new Brush Foreground
       {
           get { return block.Foreground; }
           set { block.Foreground = value; }
       }

       public new Brush Background
       {
           get { return myGrid.Background; }
           set { myGrid.Background = value; }
       }
   }

}

Note.xaml

<UserControl x:Class="WpfApplication1.Note"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="100" d:DesignWidth="250">
    <Grid Name="myGrid" >
        <Rectangle Stroke="Black" StrokeThickness="4"></Rectangle>
        <TextBlock Name="block" Margin="4"></TextBlock>
    </Grid>
</UserControl>

Modified button click event using new UserControl

private void button1_Click(object sender, RoutedEventArgs e)
{
    Note block = new Note() { Text = "Hello World", 
                              Foreground = new SolidColorBrush(Colors.Blue),
                              Background = new SolidColorBrush(Colors.PeachPuff),
                              Height=100, Width=250 };
    gridName.Children.Add(block);
    Grid.SetColumn(block, 0);
    Grid.SetRow(block, 0);
}

Need Your Help

VBA: Exiting a macro during runtime via userform button results in error 429 when restarting the macro

excel vba excel-vba userform

I have macro in Excel which extracts data from two other workbooks and imports it. During this process, a progress bar is shown and the user can quit at any time via a cancel and the standard X but...

Couldn't find Category without an ID Error message

ruby-on-rails-3.2

I'm new to Ruby on Rails, and I'm experiencing some problems

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.