I need to create a XAML textbox immediately after another textbox using code-behind. Maybe something like this:
Before
<StackPanel>
<TextBox Name="TextBox1"/>
<TextBox Name="TextBox2"/>
<TextBox Name="TextBox3"/>
</StackPanel>
After
<StackPanel>
<TextBox Name="TextBox1"/>
<TextBox Name="TextBox2"/>
<TextBox Name="InsertedTextBox"/> <!--This textbox was inserted after 'TextBox2'-->
<TextBox Name="TextBox3"/>
</StackPanel>
I have the name of the textbox I wish to insert the other textbox after. May I know how I can insert a textbox after another textbox which I know the name of?
Note: I am programming for Universal Windows.
Thanks in advance.
You need to name the StackPanel to reference it in code, and you need the index of the preceding TextBox:
var index = this.stackPanel1.Children.IndexOf(TextBox2);
this.stackPanel1.Children.Insert(index + 1, new TextBox { Name = "InsertedTextBox" });
You could try this, note I've given the StackPanel a name.
// Where 2 is the index.
this.StackPanel1.Children.Insert(2, new TextBox());
Related
Is it possible to set the Text attribute of multiple TextBlocks without calling each one separately? The possibility to iterate through them?
Like in the following example:
<TextBlock x:Name="textblock_a" Text="Original text"/>
<TextBlock x:Name="textblock_b" Text="Original text"/>
To
<TextBlock x:Name="textblock_a" Text="Modified text"/>
<TextBlock x:Name="textblock_b" Text="Modified text"/>
Probably the easiest way:
foreach(var item in new[] {textblock_a, textblock_b})
item.Text = "Modified text";
P.S.: I wouldn't use word attribute without mentioning xaml, Text is a property.
The WPF way of doing this is by using Binding.
As state HERE(easy example) you can bind the Text value of your TextBlocks to the same property.
Do not forget the INotifyPropertyChanged so everything updates when the string changes.
I am trying to solve problem with communication between items in listbox with template. Lets say i have template:
<DataTemplate x:Key="Template">
<StackPanel HorizontalAlignment="Stretch">
<TextBox Name="A" FontSize="17" Margin="5,5,5,1"></TextBox>
<TextBox Name="B" FontSize="15" Margin="5,0,5,1"></TextBox>
</StackPanel>
</DataTemplate>
And when i type data on Textbox B and press ENTER i call method that sends typed text to Textbox A. Problem is how i can create such keydown metod for template items in listbox item and what if i will heve more than one item?
Why don't you try binding like this
<DataTemplate x:Key="Template">
<StackPanel HorizontalAlignment="Stretch">
<TextBox Name="A" Text="{Binding Text, ElementName=B}" FontSize="17" Margin="5,5,5,1" />
<TextBox Name="B" FontSize="15" Margin="5,0,5,1" />
</StackPanel>
</DataTemplate>
Lets say I got it. But its kind of problematic one. Its windows 8 store app and I think I cant process with that problem like in WPF. Thats how i did it:
var parent = ((TextBox)sender).Parent;
var child = VisualTreeHelper.GetChild(parent, 1);
TextBox text = (TextBox)child;
text.Text += Client.Settings.Account + ": " + ((TextBox)sender).Text + "\n";
((TextBox)sender).Text = string.Empty;
And that one problem made another two:
When I change my template everything is going to be broken.
Lets say i want to get to the specific TextBox A in the specific Item I have no idea how to get to it. I mean i have IncomingMessage method where i need to find the item that is my contact and place his message to his TextBox A.
I have a xaml code like this
<Grid>
<... some grid row and column definitions .../>
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto">
<TextBlock "some attribute" />
</ScrollViewer>
</Grid>
I don't know how to tie Textblock with Scrollviewer in C#. I want to use Textblock with Scrollviewer.
If you have another idea, please tell me.
Many Thanks for your help. :D
just use the Content Property
var myScrollViewer = new ScrollViewer();
myScrollViewer.Content= new TextBlock();
Edit
or in combination with XAML
XAML
<ScrollViewer Name="myScrollViewer " Grid.Column="1" VerticalScrollBarVisibility="Auto"/>
Code behind
myScrollViewer.Content= new TextBlock(); // or what ever you want to add :-)
I am using a DataGrid to display some log files, where each cell contains a TextBlock. I need help creating a method to expand a user selected row like this:
This is my code right now. It is based on the index of the clicked row:
DataGridRow testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
logBrowserDataGrid.UpdateLayout();
logBrowserDataGrid.ScrollIntoView(logBrowserDataGrid.Items[index]);
testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
testrow.Height = 100;
However this creates a weird result:
Do you know a god way to expand a row based on the index?
Do you know what happens in the weird result i get? It looks like i am expanding a part of the row, and the rest does stretch out. I have also studied it in runtime, and can see that its height is the correct 100, but the ActuallyHeight is still 20.
Additional info:
The default size of the rows are set by the .RowHeight property on the DataGrid.
I am using the AutoGenerateColumns feature, plus catching the AutogeneratingColumn event to replace the column with a DataGridTemplateColumn.
Why not replace the default DataGridCellTemplate with an Expander to do all that for you?
<DataGridColumn>
<DataGridColumn.CellTemplate>
<DataTemplate>
<Expander Header="{Binding SomeText}">
<TextBlock TextWrapping="Wrap" Text="{Binding SomeText}" />
</Expander>
</DataTemplate>
</DataGridColumn.CellTemplate>
</DataGridColum>
If you don't like the default Expander look, you can overwrite it's Template to look like a plain TextBlock
As a side note, to stretch and vertically align a DataGridRow, you want to stretch and align the Cell Content, not the Row.
I tried with an Expander and the functionality was precisely what I wanted, but the look was not however. I have tried restyling the Expander to fit my need, but gave up because of the events I needed to add to it (XamlReader + events was more than my programming skills could handle). But based on Rachels suggestion I made a UserControl with the following content:
<StackPanel Orientation="Vertical" MouseUp="StackPanel_MouseUp">
<TextBlock Name="headerTextBlock" Margin="3,2,3,2" Height="20" Width="Auto" TextWrapping="NoWrap"/>
<TextBlock Name="textTextBlock" Margin="3,2,3,2" Height="Auto" Width="Auto" TextWrapping="NoWrap" Visibility="Collapsed"/>
</StackPanel>
In the codebehind I then can handle the event "StackPanel_MouseUp" to change the visibility of the TextBlocks. This control looks and works like I wanted the restyled Expander to do.
Now my xaml string looks like this.
string xamlCellTemplateFormat =
#"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
~local~>
<local:CustomExpander x:Name=""UserControlTest"" Header=""{Binding Path=~binding~}"" Text=""{Binding Path=~binding~}""/>
</DataTemplate>";
string xamlCellTemplate = xamlCellTemplateFormat.Replace("~binding~", e.Column.Header.ToString());
xamlCellTemplate = xamlCellTemplate.Replace("~local~", " xmlns:local=\"clr-namespace:IS.AppFramework.Windows.LogBrowserWPF;assembly=" + Assembly.GetExecutingAssembly().GetName().Name + "\"");
I have defined the WPF label with content="Label_Label".
While displaying it shows "LabelLabel". The first "_" is
considered for "Alt Key" Reference.
In my real requirement I am assigning Content to Label
dynamically, So please specify solution to this problem.
<Label Content="Label_Label" Height="28" HorizontalAlignment="Left" Margin="73,42,0,0" Name="label1" VerticalAlignment="Top" Width="88" UseLayoutRounding="False" ClipToBounds="False" />
If you're binding your label's content to some data and can't "escape" the underscore in the data (per mwtb's answer), then the other option is to wrap the text in a TextBlock inside the label. TextBlocks have no concept of an access key so they'll display the text as is.
So instead of this:
<Label Content="{Binding MyText}" />
You can do this:
<Label><TextBlock Text="{Binding MyText}" /></Label>
Assuming "MyText" contains the string "Hello_World", the former will display HelloWorld while the latter will display Hello_World.
Update
Per your comment, here's the same thing in code:
var tb = new TextBlock();
tb.SetBinding(TextBlock.TextProperty, new Binding("MyText"));
var label = new Label
{
Content = tb
};
That's untested but should work. Obviously you'd then have to add "label" to your visual tree in the usual manner.
You can escape the underscore by using two in a row:
Content="Label__Label"
I'm not sure what additional question you're implying by "In my real requirement I am assigning Content to Label dynamically"
Honestly, the only difference between a Label and a ContentControl is that a Label allows use of an access key. If you don't want the access key feature, just use a ContentControl.