I'm trying to make a really simple title panel, which contains an Image-icon and TextBlock-title in the center of the panel:
To achieve this with TextBlock, I could use StackPanel with Vertical-orientation. But this doesn't work when I need to add the Image in front of TextBlock.
This is the current code, which doesn't work with the TextTrimming:
<StackPanel Orientation="Horizontal">
<Image
Height="28"
Width="28"
Margin="0,0,10,0"
Source="/Resources/Images/Icons/MyIcon.png"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
<TextBlock x:Name="TitleText"
Text="Test text test text Test asd asd "
VerticalAlignment="Center"
FontSize="28"
TextWrapping="NoWrap"
TextTrimming="WordEllipsis"/>
</StackPanel>
Another solution could be to control the Width of the TextBlock in code, but I'd like to achieve this within XAML. Any ideas?
Related
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.
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();
}
I have a label and textbox which are horizontally aligned next to each other.
<TextBlock Width="400" Margin="10" Padding="10">
<Label Foreground="Yellow" FontSize="13" FontWeight="Bold" >Heading:</Label>
<TextBlock FontSize="13" TextWrapping="Wrap" FontWeight="Bold" >here is the main contetn and how about adding it to left on seco
here is the main content and how about adding it to left on seco
</TextBlock>
</TextBlock>
Can i arrange the text of the text block just below the label
A sample image is attached
I'm assuming you meant you want the heading and the text to start on the same line and overflow under the heading. You can do this by using Run elements to display text in the TextBlock you have defined.
<TextBlock Width="400" Margin="10" Padding="10" TextWrapping="WrapWithOverflow">
<Run Foreground="Yellow" FontSize="13" FontWeight="Bold" >Heading:</Run>
<Run FontSize="13" FontWeight="Bold" >here is the main contetn and how about adding it to left on seco
here is the main content and how about adding it to left on seco
</Run>
</TextBlock>
You Use Stack Panel for header And Grid with there properties Horizontal and vertical Alignments,
I want to make checkbox and textbloxk parallel or inline in my XAML WP8, but it seems the checkbox is upside and the textblock is below the checkbox. Any suggest how?
<Grid Margin="0,522,0,0" VerticalAlignment="Top" HorizontalAlignment="Left">
<StackPanel HorizontalAlignment="Center">
<CheckBox x:Name="Accept"/>
<TextBlock Text="I Accept Terms and Conditions" VerticalAlignment="Top"/>
</StackPanel>
</Grid>
You don't need TextBlock for this. Set CheckBox.Content:
<CheckBox x:Name="Accept" Content="I Accept Terms and Conditions"/>
and for other cases if want to stack elements horizontally then set Orientation="Horizontal" on you StackPanel
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
I'm currently facing huge problem i.e I'm showing Image and some text in a image the problem is when I change content property of button from code, my image disappears and only assigned text is shown, I wan to retain image and just wann change the text, Any suggetions how to handle it
<Button x:Name="btnPrescan" Margin="8" Grid.Column="2" Click="btnPrescan_Click">
<StackPanel Orientation="Horizontal">
<Image Source="Icons\Scan_Start_Icon.png" Height="14" Width="23"/>
<TextBlock x:Name="tbButtonText" Text=" Prescan"/>
</StackPanel>
</Button>
and button looks something like this
Thanks
Bind the text to a backing property on your UserControl:
<Button x:Name="btnPrescan" Margin="8" Grid.Column="2" Click="btnPrescan_Click">
<StackPanel Orientation="Horizontal">
<Image Source="Icons\Scan_Start_Icon.png" Height="14" Width="23"/>
<!-- assumes DataContext is set appropriately -->
<TextBlock Text="{Binding ButtonText}"/>
</StackPanel>
</Button>
Then just change the backing property:
this.ButtonText = "New button text";
You should do this way,
((TextBlock)btnPrescan.GetTemplatedChild("tbButtonText")).Text = "Your Text"