I have created the following piece of code that gets executed when I press an existing button made with the editor
var test = new TextBlock();
test.Text = "meep!";
test.Margin = new Thickness(50, 50, 100, 100);
FrontPage.Children.Add(test);
the code itself is working but after the "meep!" has been added to the screen all buttons created by the editor stop working, how do I fix this?
I guess you are putting the Button element inside a Grid named FrontPage as follows, and invoke the above code snippet in button click event handle. Then after you clicking the button, the TextBlock will be added to the application view but the button cannot be clicked again.
<Grid x:Name="FrontPage" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button
x:Name="btnaddtext"
Margin="117,289,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="btnaddtext_Click"
Content="Button" />
</Grid>
The reason is that if you didn't set the size for the TextBlock, it will stretch to fill the Grid, so that the TextBlock will cover the Button element that inside the Grid and it will not be clickable. TextBlock may be glassy so that you can still see the Button but cannot access. If you change TextBlock to TextBox and update the background color you may see the effects.
You have many ways to resolve this, for example, set Height and Width for the TextBlock as follows:
var test = new TextBlock();
test.Text = "meep!";
test.Height = 100;
test.Width = 100;
test.Margin = new Thickness(50, 50, 100, 100);
FrontPage.Children.Add(test);
Or change a parent layout panel, for example, to a StackPanel.
<StackPanel x:Name="FrontPage" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button
x:Name="btnaddtext"
Margin="117,289,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="btnaddtext_Click"
Content="Button" />
</StackPanel>
There're also other ways. To use which way depends on your requirements and the layout. More details about layout panel please reference layout panels.
I think you foget to add the TextBlock's algment.
You can try add the property to TextBlock and the code is below.
var test = new TextBlock()
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
test.Text = "meep!";
test.Margin = new Thickness(50, 50, 100, 100);
FrontPage.Children.Add(test);
But I think I cant care all what you mean and my code can work.
And you should know that TextBlock cant be edit. The TextBlock only can show the text.
Related
I made a button, set up his margins, height, width in my Window.xaml.cs file:
Button b = new Button();
b.Margin = new Thickness(10, 10, 10, 10);
b.Content= "Button";
b.Height = 50;
b.Width = 50;
I want it to show up on my Window.xaml window when i start the program.
Using only these few properties is not showing it.
I could go in Window.xaml file and type in grid or wherever:
<Button Margin="10,10,10,10" Content="Button" Height="50" Width="50"></Button>
And it would show this button in the widnow, but using only these properites (in .cs code), is not enough
I assume you only instantiated the button. The button doesn't know where it should be on the screen. It must be assigned to a named parent control like a grid or stackpanel.
XAML:
<Window>
<Grid x:Name="RootGrid">
</Grid>
</Window>
Code behind:
RootGrid.Children.Add(b);
My goal is to attach a new image control while the application is running.
img = new System.Windows.Controls.Image();
img.Margin = new Thickness(200, 10, 0, 0);
img.Width = 32;
img.Height = 32;
img.Source = etc;
I've tried
this.AddChild(img);// says must be a single element
this.AddLogicalChild(img);// does nothing
this.AddVisualChild(img);// does nothing
It was never this difficult to add a element with forms.
How can I simply attach this new element to the main window (not another control) so that it will show up.
Solved it, I named the grid main, and from there I was able to access the children attribute and the add function
main.children.add(img);
<Window x:Class="Crysis_Menu.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" Loaded="Window_Loaded" AllowsTransparency="False" Background="White" Foreground="{x:Null}" WindowStyle="SingleBorderWindow">
<Grid Name="main">
<Button Content="Run" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="btnRun" VerticalAlignment="Top" Width="151" Click="btnRun_Click" />
<TextBox Height="259" HorizontalAlignment="Left" Margin="12,40,0,0" Name="tbStatus" VerticalAlignment="Top" Width="151" />
</Grid>
</Window>
You should have only one root element under window. Adding the image using this.AddChilda adds the image as child of window, but you probably have some other child defined(Grid for example). Give a name to this child (Grid in the example) and then in the code behind add the image to the Grid
Example :
<Window>
<Grid x:Name="RootGrid">
</Grid>
</Window>
Then in the code behind use
RootGrid.Children.Add(img);
What is this in your case? You can try this.Content = image; or this.Children.Add(image);
If your this is indeed a Window, you should know that Window can have only a single child, which you put into Content. If you want several items in Window, usually you put some appropriate container (for example, Grid or StackPanel) as Window's content, and add children to it.
Vlad got the solution. I used it :
var grid = this.Content as Grid;
// or any controls
Label lblMessage = new Label
{
Content = "I am a label",
Margin = new Thickness(86, 269, 0, 0)
};
grid.Children.Add(lblMessage);
I have created a TextBox above a Path-Element (the Path Element draws a rectangular thing, which acts as the design of the textbox). Now I want to disable this TextBox with
valCon.ValueTextBox.IsEnabled = false;
This works so far. However, since I don't want the TextBox to have any Style (no Color, no Borders, etc.), but only a visible Text in it, I'm getting a small problem:
When the TextBox is disabled, it automatically receives a style which I can't get rid of. The Background changes to white, the Opacity changes to around 0.3 and Borders appear.
I can't seem to solve this problem by adding
valcon.ValueTextBox.Background = new SolidColorBrush(Colors.Transparent);
valcon.ValueTextBox.BorderBrush = new SolidColorBrush(Colors.Transparent);
etc.
It just seems to ignore this. Does anyone know a solution?
Greetings
Narakuvera
You need to take control over the template to achieve the same
here is a basic template for you with no border and no background TextBox
<TextBox Text="hello">
<TextBox.Template>
<ControlTemplate TargetType="TextBox">
<ScrollViewer Margin="0"
x:Name="PART_ContentHost" />
</ControlTemplate>
</TextBox.Template>
</TextBox>
you can choose to set IsEnabled="False" and it will still remain border less
Code behind approach
ControlTemplate ct = new ControlTemplate(typeof(TextBox));
FrameworkElementFactory sv = new FrameworkElementFactory(typeof(ScrollViewer));
sv.Name = "PART_ContentHost";
ct.VisualTree = sv;
textbox1.Template = ct;
WinRT code behind approach
string template = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TargetType=\"TextBox\"><ScrollViewer Name=\"PART_ContentHost\" /></ControlTemplate>";
ControlTemplate сt = (ControlTemplate)XamlReader.Load(template);
textbox1.Template = сt;
I have a Customer Pages which contains information of customer name. This page contains a StackPanel and inside this StackPanel there is a ScrollViewer and inside the ScrollViewer there is another StackPanel.
I am adding number of StackPanels dynamically. Inside the StackPanel I am adding TextBlock dynamically behind, which contains text. Then add InkPresenter to show a separation.
My problem is after adding every time when I try to use the page, it doesn't scroll. In fact if I swipe up the page goes down and come back.
XAML :
<StackPanel>
<ScrollViewer Margin="0,-20,0,0" Grid.RowSpan="2" >
<StackPanel Height="Auto" x:Name="pottilelist" >
</StackPanel>
</ScrollViewer>
</StackPanel>
StackPanel stk = new StackPanel();
stk.Name = g.id.ToString();
stk.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(Customer_Click);
TextBlock tbx = new TextBlock();
tbx.Text = g.customername;
tbx.Name = "A" + g.id.ToString();
tbx.FontSize = 36;
tbx.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(Customer_Click);
tbx.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
stk.Children.Add(tbx);
InkPresenter ink = new InkPresenter();
ink.Height = 4;
ink.Background = Brush3;
ink.Margin = new Thickness(0, 0, 0, 20);
stk.Children.Add(ink);
pottilelist.Children.Add(stk);
In one app it worked, but in another it doesn't.
Try to put the scrol viewer around the Layout Grid (main grid) that contains all page contents. I think this should solve the problem.
As long as you are using Pivot Template , just put it after the deceleration of that pivot item around the grid and it should works !
<phone:PivotItem Header="PivotItem">
<ScrollViewer>
<Grid x:Name="LayoutRoot" Background="#7F000000" >
It depends on where your stackpanel is placed inside the grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1">
<ScrollViewer>
<StackPanel></StackPanel>
</ScrollViewer>
</StackPanel>
</Grid>
In above code scrollviewer will work as expected, because of the height.
If you have set the height of the position in its visual tree, then scrollviewer might not scroll.
I have solved the problem. This problem occurs when there is no height for a stackpanel or scrollviewer. Height Issue
I cannot understand why in the hell this simple silverlight application freezes up. Here is the code:
namespace test
{
public partial class MainPage : UserControl
{
TextBlock txtword;
public MainPage()
{
InitializeComponent();
txtword = new TextBlock();
txtword.Text = "TEST";
LayoutRoot.Children.Add(txtword);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
txtword.Text = "SuperDuper";
}
}
}
After the textblock is added to the layoutroot if you try to hover or click on the button you can tell that the app has frozen for some reason. Any idea what is going on here??
If i add the text block in the XAML i am able to change its text property in the button click. LayoutRoot.Children.Add() is causing the app to freeze..
From reading your comments it seems the XAML in MainPage.xaml is something like the following:
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Do stuff" Click="Button_Click" />
</Grid>
After adding the TextBlock, either in code or in XAML, you effectively end up with the following:
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Do stuff" Click="Button_Click" />
<TextBlock Text="TEST" />
</Grid>
Your Grid doesn't specify any ColumnDefinitions or RowDefinitions, so you have a 1 × 1 grid with all child controls of the Grid given the entire width and height of the grid.
As neither your Button nor your TextBlock specify a z-index value (using Canvas.ZIndex), their z-order is defined by their position within the grid's Children. The TextBlock comes after the Button, so it is the one that is 'on top'.
The TextBlock may contain only a tiny amount of text, but the TextBlock itself will still fill the Grid. TextBlocks do not automatically resize to fit the text they contain and nothing else. Your Button appears not to work because the TextBlock is on top of it and receives all of the mouse events. TextBlocks are static controls that do nothing in response to any mouse event, and this should explain why your app is appearing to freeze.
Setting the HorizontalAlignment and/or VerticalAlignment of the TextBlock to a value other than Stretch stops the TextBlock being given the entire width and height of the Grid and allows the Button to receive mouse events.