0

我需要放在TextBlock一个容器内,例如一个网格为了放置背景颜色。我知道这在 xaml 中是如何工作的,但是每次单击按钮时都会C# -> (xaml.cs)生成 a 。因此,一个不是最初创建的。TextBlockSaveClose

        //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"/>

我希望每个新textBlock创建的都具有相同的背景,我该怎么做?谢谢

4

1 回答 1

0

在这种情况下,一张照片会有所帮助。我假设您想知道如何将 TextBlock 添加到网格中。

首先,您需要为网格命名,以便在 CodeBehind 中引用它。然后只需将您的 TextBlock 添加到 Grids Children Collection 中。(我在这里使用的是 gridName 的名称)。

gridName.Children.Add(block);

如果您的 Grid 有行和列,您将需要使用附加属性将控件分配到正确的位置。(我使用 rowId 和 columnId 来指定放置控件的 Grid 的行号和列号。

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

把它放在一起:

主窗口.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>

使用用户控件的示例。

注意.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; }
       }
   }

}

注意.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);
}
于 2012-12-28T08:01:58.060 回答