我需要将TextBlock
放入容器中,例如网格为了放置背景颜色。我知道这在xaml中是如何工作的,但是C# -> (xaml.cs)
每次点击TextBlock
按钮时都会生成SaveClose
。因此,最初不会创建一个。
//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
具有相同的背景,我该如何解决这个问题?
感谢
答案 0 :(得分:0)
在这种情况下,图片会有所帮助。我假设你想知道如何将TextBlock添加到网格。
首先,您需要为网格指定一个名称,以便在CodeBehind中引用它。然后只需将TextBlock添加到Grids Children Collection。(我在这里使用了gridName的名称)。
gridName.Children.Add(block);
如果您的Grid有行列,则需要使用附加属性将控件分配到正确的位置。(我使用rowId和columnId来指定控件放置的Grid的行号和列号。
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);
}
}
}
<强> 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>
使用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);
}