2010-08-29 27 views

risposta

30

Rectangle non ha alcun contenuto bambino, quindi sarà necessario mettere entrambi i controlli all'interno di un altro pannello, come ad esempio una griglia:

<Grid> 
    <Rectangle Stroke="Red" Fill="Blue"/> 
    <TextBlock>some text</TextBlock> 
</Grid> 

È inoltre possibile utilizzare un controllo di frontiera, che si terrà un singolo bambino e disegnare un rettangolo intorno ad esso:

<Border BorderBrush="Red" BorderThickness="1" Background="Blue"> 
    <TextBlock>some text</TextBlock> 
</Border> 

si dice "rettangolo dinamico", così suona come si sta facendo questo nel codice. L'equivalente C# sarebbe simile a questa:

var grid = new Grid(); 
grid.Children.Add(new Rectangle() { Stroke = Brushes.Red, Fill = Brushes.Blue }); 
grid.Children.Add(new TextBlock() { Text = "some text" }); 
panel.Children.Add(grid); 
// or 
panel.Children.Add(new Border() 
{ 
    BorderBrush = Brushes.Red, 
    BorderThickness = new Thickness(1), 
    Background = Brushes.Blue, 
    Child = new TextBlock() { Text = "some text" }, 
}); 

Ma se volete un elenco dinamico di rettangoli, probabilmente si dovrebbe utilizzare un ItemsControl:

<ItemsControl ItemsSource="{Binding}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border BorderBrush="Red" BorderThickness="1" Background="Blue"> 
       <TextBlock Text="{Binding Text}"/> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

Se si imposta il DataContext ad una lista di oggetti , questo XAML creerà un bordo con un TextBlock per ognuno con il testo impostato sulla proprietà Text sull'oggetto.

+1

Potrebbe fornire alcuni frammenti di codice per Binding del datacontext di Itemcontrol. – Lalchand

1

È necessario aggiungere un controllo testuale allo StackPanel, ad esempio Label o TextBlock.

+0

Ho bisogno di aggiungere del testo all'interno di Rectangle. Come posso aggiungere Etichetta nel rettangolo. – Lalchand

2

Prima di tutto è possibile farlo, ma non aggiungendo il controllo. E c'è una buona ragione per farlo, per il rendering hardware ad alta velocità. È possibile creare un pennello speciale da un elemento dell'interfaccia utente che si memorizza nella cache nell'hardware e riempire il rettangolo con questo hardware ed è estremamente veloce. Mostrerò il codice solo perché è l'esempio che ho di seguito

Rectangle r = new Rectangle(); 
r.Stroke = Brushes.Blue; 
r.StrokeThickness = 5; 
r.SetValue(Grid.ColumnProperty, 1); 
r.VerticalAlignment = VerticalAlignment.Top; 
r.HorizontalAlignment = HorizontalAlignment.Left; 
r.Margin = new Thickness(0); 
r.Width = 200; 
r.Height = 200; 
r.RenderTransform = new TranslateTransform(100, 100); 
TextBlock TB = new TextBlock(); 
TB.Text = "Some Text to fill"; 
//The next two magical lines create a special brush that contains a bitmap rendering of the UI element that can then be used like any other brush and its in hardware and is almost the text book example for utilizing all hardware rending performances in WPF unleashed 4.5 
BitmapCacheBrush bcb = new BitmapCacheBrush(TB); 
r.Fill = bcb; 
MyCanvas.Children.Add(r); 
Problemi correlati