I am trying to implement a TextWrapping feature onto the text that is displayed in a CheckBox. I am using the second method in this walk through.
I have created a completely blank program to test this. I would like to make it so that, when text inside the checkBox is larger than the allowed width, it moves the excess text to the next line under checkBox.
This is my xaml:
<Grid Width="100">
<CheckBox>
<AccessText TextWrapping="Wrap"
Text="This is a really long sentence that needs to create a new line." ... />
</CheckBox>
</Grid>
The walk through uses this method to implement the result that I am looking for. Why isn't this working for me, and how do I implement it correctly?
This is what my output is:
Updates: TextWrapping changed to "Wrap"; Width removed; output updated
Related
I'm trying to highlight part of text in a textblock from a listbox datatemplate which in turn is bounded to a property of a custom class by using a textbox to search the list for input text. But the problem is that only part of the items are highlighting (most of the ones visible) but when i maximize the window and try to input another character then suddenly all of them gets highlighted my guess where the problem might be is in this piece of code:
ListBoxItem listboxItemFound= (ListBoxItem)this.listBox1.ItemContainerGenerator.ContainerFromItem(TItem);
Since this method is returning a null when the items are not visible but the items are currently in the listbox. Somehow I guess the items listboxItem instances are not yet created until you scroll down or maximize to view more items.
XAML DataTemplate:
<DataTemplate>
<Grid Name="gridOfListbox" Height="25" Margin="0,2">
<DockPanel Name="dockpanelWithTxtBlock">
<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
<Run Text="" /><Run Background="Yellow" Text="" /><Run Text="{Binding ProductID}" />
</TextBlock>
</DockPanel>
</Grid>
</DataTemplate>
If more code is needed just let me know.
Any help would be greatly appreciated!!
Also if there is any other better way of finding the listboxItem bounded to the custom Item just let me know. Thank you very much!
[Pic of problem] http://i.stack.imgur.com/HViag.png
One way to fix this is to set VirtualizingStackPanel.IsVirtualizing to false for your ListBox. This will cause all of the items to be created right away. The downside to this is if your ListBox has many items, your program will use more memory (since more items will be created), and could potentially run slower depending on the number of items.
A better solution to consider would be to have multiple DataTemplates for this - one without the highlight, and one with. You can set a DataTemplateSelector for your ListBox (using the ItemTemplateSelector property). The selector can choose which template to use based on if the item matches the search term or not.
The tricky part would be writing the template with the highlighted text. You could probably achieve that by having properties on the object the ListBoxItem is bound to for the text before the highlighted text, the highlighted text, and then the remaining text.
I need to use two textblock within a single panorama item. I have used a grid to implement this.
Problem is that I need to change the text of the textblock programmatically.
<phone:PanoramaItem Header="Current Status">
<Grid >
<TextBlock x:Name="Spent" Margin="138,0,90,464" FontSize="40"/>
<TextBlock x:Name="Left" Margin="138,50,90,414" FontSize="40"/>
</Grid>
</phone:PanoramaItem>
Saying:
Spent.Text = Convert.ToString(total_spent);
Left.Text = Convert.ToString(total_left);
in the c# file gives an error.
Please tell me how to change the text of the individual textblocks :)
P.S:
I am an absolute beginner and almost completely self taught, so a simple answer will be useful
Thanks
You need to use Text property of the TextBlock to get/set the Text.
From MSDN : TextBlock.Text
Gets or sets the text contents of a TextBlock.
Try This:
Spent.Text = Convert.ToString(total_spent.Text);
Left.Text = Convert.ToString(total_left.Text);
when i use more than one controls inside the wrap Panel if any control contains lengthy text (more than the window size) cut-off is happening. (See the image)
I have two textblocks and one button control.
<Grid>
<WrapPanel>
<TextBlock Text="Very long Text Message contains long text for testing " FontWeight="Bold"></TextBlock>
<Button Content="sample Text"></Button>
<TextBlock Text="sample Text textblock"></TextBlock>
</WrapPanel>
</Grid>
the cut-off happens for first text block. i want wrap to next line if the text contains more characters.
help me to solve the issue. thanks in advance.
You need the attribute TextWrapping="Wrap" in your textbox
<TextBlock
Text="Very long Text Message contains long text for testing "
FontWeight="Bold"
TextWrapping="Wrap">
</TextBlock>
Are you trying to make a single paragraph that wraps? If so, then WrapPanel isn't actually what you want.
WrapPanel takes UI elements (rectangular chunks of real estate) and lays them out left-to-right, top-to-bottom. You could enable wrapping in your first TextBlock, but then it'll take up a rectangle of screen space that's two lines high. Because the TextBlock fills that entire rectangle, the button will actually appear under it, rather than to the right of the bold words "for testing ".
If you want to make the whole thing flow like a paragraph, you don't want to use UI elements (which are always rectangular chunks); you want to use text elements (which flow in paragraphs).
The way to get text elements into your XAML is to wrap them in a TextBlock. Try this:
<Grid>
<TextBlock TextWrapping="Wrap">
<Bold>
<Run Text="Very long Text Message contains long text for testing " />
</Bold>
<Button Content="sample Text"></Button>
sample Text textblock
</TextBlock>
</Grid>
Note that I wrapped the first chunk of text in a <Run> element -- otherwise the trailing space would be ignored (assumed to be whitespace in your XAML document). The second chunk of text didn't have leading or trailing spaces, so I just put it directly inline.
This answer has a bit more about the difference between the "controls" and "text" sides of XAML.
I have a combobox in a wpf c# application.
What i am trying to do is the following.
I have a unselected combobox, as you look at it i can see an arrow to the right hand side and a space for text on the left. For the purpose of this question i'll refer to this text as 'Cell Text'.
When you select the button it appears with a list. I want this list to contain a number of robots my GUI/PC can connect to. When i select a robot, a message is sent to this robot trying to connect with it.
The 'Cell Text' i want to display the name of the currently connected Robot. There will be situations when a connection to a selected robot would'nt be possible, also a successful connection could take 5 seconds.
What i need to do is stop the selection automatically appearing in the 'Cell Text', is this possible?
Thanks
<ComboBox ItemsSource="{Binding MyRobotOptions}" Grid.Column="1" SelectedItem="{Binding SelectedRobot}" Margin="5"/>
For an inexperienced user (no offence), one of the easiest ways you can do this is to overlay a TextBlock over the 'Cell Text', as you call it:
<Grid>
<ComboBox ItemsSource="{Binding MyRobotOptions}" Grid.Column="1" SelectedItem="{
Binding SelectedRobot}" Margin="5" />
<TextBlock Text="{Binding YourSelectedRobotName}" Background="White"
Margin="0,0,24,0" />
</Grid>
I haven't been able to test this, so you might need to adjust the Margin property values to make it fit better, but it should hide the original text value.
A better solution I think is to use a separate indicator for connection state. For example a colored border around the Combobox that turns green when connected, red when disconnected. That way you don't have to break the paradigm of a ComboBox that everyone assumes: when you select something it immediately appears selected in the ComboBox.
I have string which i have to display in TextBlock, my TextBlock have some fixed size, i need display the text in such manner if string cannot fit in TextBlock, then i have to split the string in next TextBlock, how can i do this same.
Why don't you try using the TextWrapping property of that TextBlock?
XAML:
<TextBlock TextWrapping="Wrap" Text="very very very long text" Width="30"/>
C#:
myTextBlock.TextWrapping = TextWrapping.Wrap;
If you don't want wrapping, then slapping on a horizontal/vertical scrollbar is another option that you may want to explore. Reading the question I think textwrapping might be more appropriate (doesn't sound like you want to hide anything), but options are always nice.
<ScrollViewer Height="30">
<TextBlock Width="30" TextWrapping="Wrap">HElooooooooooooooooooooooooooooooooooooo</TextBlock>
</ScrollViewer>
EDIT: Combines a word wrap and a scrollviewer.