Accessing and using a datatemplate textbox in xaml.cs - c#

Normally when I create a textbox I can use it in visual studios like so.
xaml
<TextBlock Name="BedNumberTextBlock" FontSize="26" Foreground="White" FontFamily="Segoe UI" HorizontalAlignment="Left" Margin="10,0">
c#
BedNumberTextBlock.Visibility =Visibility.Collapsed;
However when my textblock is enclosed in a data template I cannot find it to control its visibility. Could some one show me how to find the textblock and collapse its visibility please?

Related

Implementation for entering a password: How to click on the TextBox located under the TextBlock?

I'm trying to make an alternative way for a password binding (I know there are different ways)
I put a textblock and a textbox in the same location
The user will write inside the textbox, its foreground will be transparent
The textblock will bind with the length of the textbox's text and show "*"s according to the length.
when I'll hold down some "eye icon" the textblock will not be visible and the the textbox's foreground will be black
The problem is that when I put them both together, the block blocks the box and I can't write in it.
Maybe it's just a property I didn't find, "priority" or something
Would like a suggestion on what should I do, thank you :)
The problem is that when I put them both together, the block blocks the box and I can't write in it.
Maybe it's just a property I didn't find, "priority" or something
IsHitTestVisible property.
Example:
<StackPanel>
<TextBox Text="1234"/>
<Grid>
<TextBox x:Name="textBox" Text="1234"/>
<TextBlock Text="***********" Background="Wheat"
IsHitTestVisible="False"/>
</Grid>
<TextBlock Text="{Binding Text, ElementName=textBox}"/>
</StackPanel>
BUT!!!
In my opinion, it is easier to make the TextBox transparent.
In such an implementation, you can see where the input cursor is in the field.
Example:
<StackPanel>
<TextBox Text="1234"/>
<Grid>
<TextBlock Text="***********" Background="Wheat"/>
<TextBox x:Name="textBox" Text="1234"
Background="Transparent"
Foreground="Transparent"
BorderThickness="0"/>
</Grid>
<TextBlock Text="{Binding Text, ElementName=textBox}"/>
</StackPanel>
Only you should change controls code position!
change

WPF textBox changes color halfway

I have a strange problem with the textBox in WPF. I have a simple textbox inside a stackpanel binded with some text on the code behind. When the text becomes too long some part of it changes color. Here is an example
The stackpanel is this
<StackPanel Canvas.Left="50" Canvas.Top="111">
<TextBlock Text="{Binding Title}" FontSize="32" Foreground="White" />
<TextBlock Text="{Binding Subtitle}" FontSize="15" Foreground="#9EA3AA"/>
</StackPanel>
How can I make all the text white?
As mentioned by Clemens you are experiencing overlap. To fix this make the cell containing the orange cpu image and text not extend so high. You should be able to just drag and drop.

WPF Textblock With Bound Text Will Not Scroll

The text in my TextBlock is bound to an element in my code. However, when I first open my window, the Textblock is completely empty. As I add text, I need the ScrollViewer to allow me to scroll down the text, or automatically scroll down as more text is added. Using the MVVM, so no code behind would be ideal.
<StackPanel Grid.Column="0" Grid.Row="1" Margin="0 10">
<Label Style="{StaticResource LabelStyle}">Output</Label>
<ScrollViewer VerticalScrollBarVisibility="Visible" Height="100">
<TextBlock ScrollViewer.CanContentScroll="True" Height="100" VerticalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding Path=ProcessingOutput}"/>
</ScrollViewer>
</StackPanel>
How can I make this happen? Is there a way to update the ScrollViewer so that it sees more text is beyond what I can see in the TextBlock and allows the user to scroll down, or allows me to set an autoscroll feature that scrolls down automatically as text is added via binding?
Thanks in advance!
scrollbar will work if you remove Height="100" from TextBlock
to make it scroll when Text changes other answers suggest to use ScrollViwer.ScrollToBottom() method, e.g. like this:
<ScrollViewer Name="scroll"
VerticalScrollBarVisibility="Visible"
Height="100">
<TextBlock ScrollViewer.CanContentScroll="True"
VerticalAlignment="Stretch"
TextWrapping="Wrap"
Text="{Binding Path=ProcessingOutput, NotifyOnTargetUpdated=True}"
TargetUpdated="Txt_OnTargetUpdated">
</TextBlock>
</ScrollViewer>
private void Txt_OnTargetUpdated(object sender, DataTransferEventArgs e)
{
scroll.ScrollToBottom();
}

WPF DockPanel: cannot select it to add children

I have created a few text boxes that have to be added as children to my DockPanel. I have given this DockPanel a name and then when I try in my code to use this name, Intellisense does not give me any results. How do I select DockPanel and then add its children?
I want to select it and add some text boxes to it as its children. For example: nameLocationPanel.Children.Add(new TextBox());
This is the top part of my XAML code that contains the DockPanel. It already has some text boxes, but I want to add more in my C# code.
<ListBox ItemContainerStyle="{StaticResource lbStyle}" Name="searchList" BorderBrush="#FF898989" BorderThickness="2" MouseUp="searchList_MouseUp" MouseDoubleClick="searchList_MouseDoubleClick" Visibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<DockPanel Name="nameLocationPanel">
<TextBlock FontSize="14" HorizontalAlignment="Left" Text="{Binding Path=FirstName}"/>
<TextBlock FontSize="14" HorizontalAlignment="Left" Text=" "/>
<TextBlock FontSize="14" HorizontalAlignment="Left" Text="{Binding Path=LastName}"/>
<TextBlock HorizontalAlignment="Right" Text="{Binding Path=Location}"/>
</DockPanel>
It is within a DataTemplate, you can't select it by name at all as it doesn't exist in your Window, it's just a Template definition that will get repeated so there isn't 1 dockpanel but N, N being equal to how many items there are in your listbox and there could well be 0 dockpanels.
What exactly are you trying to achieve? Odds are you're going about it backward.
You cannot access to the DockPanel because it is a part of a DataTemplate. It is generated for every item in your ListBox so it is impossible to access to a single instance of it.
To add additional items to the DockPanel it is necessary to do it via something like a binding in XAML or a special UserControl. But it depend always on the ListBoxItem.

XAML WP8 and making a 'TextBlock' scroll down

Alright, so I have a XAML page with a TextBlock in a Windows Phone 8 application. My dilemma is this:
I pragmatically add more content (formatted lines with Inlines.Add(new Run...) to the TextBlock. The text block is currently filled from bottom to up because of the ScrollViewer in the sense that a line appears in the bottom after another. I would also be fine with them starting to appear from the top as long as the TextBlock would continue to scroll down (actually this might look better) once it is full. My TextBlock is inside a ScrollViewer as below:
<Popup x:Name="send_message_xaml" Grid.Row="1" VerticalOffset="0" Width="750" Height="auto">
<StackPanel Orientation="Vertical" Margin="0,0" Width="auto" Height="auto">
<ScrollViewer Height="345" HorizontalAlignment="Left" Margin="0,0,0,0" Name="scrollViewer1" VerticalAlignment="Bottom" Width="420" VerticalScrollBarVisibility="Auto">
<TextBlock x:Name="message_log" Margin="40,50,0,0" TextWrapping="Wrap" Width="420" VerticalAlignment="Top" FontSize="22"/>
</ScrollViewer>
<TextBox x:Name="message_to_send" InputScope="Chat" Width="480" KeyDown="message_to_send_KeyDown" Margin="15,0"/>
</StackPanel>
</Popup>
How can I get the textblock to scroll so that the newest appended message is always at the bottom? I found a bunch of these threads but none seem to solve my problem so far. Do I need to add some code somewhere with the appending?
You need to update the VerticalOffset based on the ScrollableHeight. When you add new inlines to the TextBlock, its height is going to change and that will notify the parent ScrollViewer. So, after you add new items to the inlines, run the Measure method and update the VerticalOffset.
Here is an example.

Categories

Resources