如何把文本块在容器中添加背景颜色
本文关键字:添加 背景 颜色 文本 | 更新日期: 2023-09-27 17:49:39
我需要把TextBlock
在一个容器内,例如一个网格,以便把背景颜色。我知道这在xaml中是如何工作的,但在C# -> (xaml.cs)
中,每次单击SaveClose
按钮时,都会生成TextBlock
。因此,人不是最初被创造的。
//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="✔" AutomationProperties.Name="Save and Close" Click="SaveClose_Click" />
<Button Name="Delete" Style="{StaticResource AppBarButtonStyle}" Content="" 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"/>
我想要每一个新的textBlock
创建有相同的背景,我该怎么做?由于
在这种情况下,图片会有所帮助。我假设你想知道如何将你的TextBlock添加到网格。
首先,你需要给你的网格一个名字,这样它就可以在CodeBehind中被引用。然后简单地将你的TextBlock添加到grid Children Collection中。(我在这里使用gridName的名称)。
gridName.Children.Add(block);
如果您的网格有行和列,则需要使用附加属性将控件分配到适当的位置。(我使用rowId和columnId来指定要放置控件的网格的行号和列号。
Grid.SetColumn(block, columnId);
Grid.SetRow(block, rowId);
把它放在一起:
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);
}
}
}
主窗口。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>
使用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>
修改按钮点击事件使用新的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);
}