Reputation: 5175
here is the XAML...
<Canvas Name="myCanvas">
<TextBlock Name="myBlock" FontFamily="Arial Black" FontSize="100" Foreground="Red" Text="R" Height="105" Width="96" Canvas.Left="61" Canvas.Top="80" /
</Canvas>
I have a partial class that extends a userControl.
public partial class Card : UserControl
I also have a test form that uses this control like this,
public formTest()
{
InitializeComponent();
Card1.drawText();
myCanvas.Children.Add(Card1); //myCanvas is defined in XAML
}
Card Card1 = new Card();
How do add an instance of TextBlock to myCanvas when TextBlock is inside my UserControl? So lets say,
public partial class Card : UserControl
{
private TextBlock txtBlock = new TextBlock();
public Card()
{
txtBlock.Text = "Test";
txtBlock.Foreground = brushFill;
}
public void drawText()
{
//uhhh idk
}
}
In general I don't understand how to get anything to display without defining it in the XAML then adding properties via code. Like this I create an instance of TextBlock, give it some properties... then I'm not sure.
Any help is appreciated. I also know I should be using a User Control but I don't know why?
Upvotes: 1
Views: 3031
Reputation: 950
If you just want to create a simple TextBlock and add it to the canvas, you'll want to do something like this:
TextBlock textBlock = new TextBlock();
textBlock.Text = "Text";
myCanvas.Children.Add(textBlock);
Then you can manipulate everything you have added to the Canvas via methods in Canvas.Children.
Upvotes: 1
Reputation: 273179
Your UserControl should also have a XAML file associated, and the TextBlock should be inside that XAML.
The other option would be Card : Control
(not UserControl) and then you would need a template.
In neither coase can/should you try to add a TextBlock from inside a Control to myCanvas.
Seems you ought to read up on WPF UserControls and Custom Controls.
Upvotes: 1