I'm working on a scorekeeping app in WPF for Darts, and part of it is to display the score, which the player has thrown. I'm trying to output it on the window using dynamically created labels like this:
Label throwLabel = new Label();
throwLabel.Content = score.ToString();
throwLabel.Style = Resources["BoardStats"] as Style;
throws.Children.Add(throwLabel);
Where throws is a Stack Panel and BoardStats is a style defined in app XAML. The problem is that when the label appears on the window, it doesn't have the style I have given it in the code above. How can I fix this?
Related
I am having some trouble creating the template of my application :
I am making some dynamic parts of forms and I use a panel in which I include my User Control according to the choice of the user. The panel has the same size as the user control but it doesn't work at runtime. The panel in which the UC is included is inside another UC also.
I tried autoSize = true, fixed size and many things but I don't know why it goes like this. Any idea?
Some parts of my code to load the User Control :
UC_panel.Controls.Clear();
UC_ADDRESS uca = new UC_ADDRESS();
uca.Dock = DockStyle.Fill;
uca.AutoSize = true;
UC_panel.Controls.Add(uca);
My UserControl for the address
At runtime it goes bigger
Since you set
uca.Dock = DockStyle.Fill;
and also
uca.AutoSize = true;
(which is redundant)
it is possible that some other container has its AutoScaleMode set to Font and its font size is bigger.
I'm just learning how to make universal apps for windows 10. Most of the tutorials show you how to work with XAML and how to assign functions to elements when you click on them but I couldn't find a way to make a new control appear when I click a button.
I'm making a note taking application. I've designed most of the stuff that I need. Now I want whenever I click a button to create a new textblock where the user can write their note.
//Create a new note when clicking the add button
private void newNoteBtn_Click(object sender, RoutedEventArgs e)
{
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
}
This is the code that runs when the button is clicked. When I run the application nothing happens. I think I have to put the new textbox in some kind of Grid or something.
Most of the tutorials are really old or mention windows forms and use some sort of this.Controls.Add(newNote); but Visual studio doesn't give me the Controls.Add option. I've also created a <Grid x:Name="notes"></Grid> which I thought I could use as a placeholder for the notes that are being created but I can't access the Grid element through the code.
Container Controls like Grid have Children property so you should use Childern like this:
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
notes.Childern.Add(newNote);
When defining
<Grid x:Name="notes"></Grid>
in XAML on the page, you be able to use notes as the identifier to access this Grid from the page's code behind:
notes.Children.Add(newNote);
In Windows Forms, I have created a Twitter application that gets the latest tweets from the Timeline using TweetSharp. I have used a UserControl and a Panel to display these. The code I've used to do this looks like this:
IEnumerable<TwitterStatus> tweets = service.ListTweetsOnHomeTimeline();
foreach (var tweet in tweets)
{
TweetBox t = new TweetBox();
t.username.Text = tweet.User.ScreenName; // Label
t.display.ImageLocation = tweet.User.ProfileImageUrl; // PictureBox
t.tweet.Text = tweet.Text; // Label
t.info.Text = tweet.CreatedDate.ToString();
t.Dock = DockStyle.Top;
HomeTimeline.Controls.Add(t); // Add to HomeTimeline Panel
}
I'm now re-making this application in WPF. The UserControl is in the same format, but I'm clueless as of how to add this to the panel and dock it to the top - or the equivalent in WPF. How do I do this?
A WinForms UserControl are not the same as a WPF UserControl.
You'll have to make the UserControl again, in WPF.
Of course, you could wrap the WinForms UserControl in a WindowsFormsHost element, but that's not good practice.
I want to add a WPF Input Box in my Project in C#. I got a WinForm one from InputBox in C# but it has Winform look and feel. So i was recreating it in WPF. I have created all the controls (Label, Button, Textbox) but i am unable to add them to my window.
static Window winInputDialog
The Window is showing through ShowDialog but without controls.
There are two ways to get controls in your window:
Do the whole designing stuff in the Designer of VisualStudio
Add the controls by code. Here is a short, simple sample of creating a window and putting controls in it:
var window = new Window();
var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
stackPanel.Children.Add(new Label { Content = "Label" });
stackPanel.Children.Add(new Button { Content = "Button" });
window.Content = stackPanel;
I want to arrange the controls in a Panel control. For example, I have four labels and four textboxs, I want to put them in a panel like a table without using the VS designer, only use the code. Does anyone do it before?
Best Regards,
C#, and use styles to control layout.
Panel pnl = new Panel();
Label lbl1 = new Label();
lbl1.Text = "1";
pnl.Controls.Add(lbl1);
TextBox tb1 = new TextBox();
pnl.Controls.Add(tb1);
Page.Controls.Add(pnl);
label
{
display: inline;
}
You could essential do the same thing Visual Studio does on the back end. Create a new control and set the properties, such as: size, name, text, and location.
I think that you can simply create a custom control that contains a label and a text box. Let's call this control LabeledTextBox. And then on your code simply add 4 instances of LabeledTextBox one after the other. This should provide the look and feel you want.