I want to get the content of a graphical elements present in a WPF TabItem.
Consider the following code for the window :
<TabItem x:Name="TheItem" Header="Form">
<Grid x:Name="MainGrid">
<TextBox x:Name="txtContent" Text="Hello I'm some content !" />
<TextBox x:Name="txtOther" Text="Some other content" />
</Grid>
</TabItem>
I've looked up the TabItem documentation on MSDN but failed to find any useful information.
Is there any way to get the content from the "txtContent" textbox from a TabItem ?
Try this:
Grid grid = TheItem.Content as Grid;
TextBox txtContent = grid.Children[0] as TextBox;
string text = txtContent.Text;
Or simply:
TextBox txtContent = MainGrid.Children[0] as TextBox;
Related
I want to add a flyout to my TextBlock, and when I select the text in the TextBlock, the flyout will show up at the selected (kind of like Reading Mode in Microsoft Edge, when you select the text in reading mode, there will be a flyout show the word's definition). But I don't know how. I've tried using the SelectionChanged, but the parameters that this event passes don't have an position that I can use to set the flyout. So how can I do that?
Besides, I'm wondering what SelectionFlyout is for? I thought it could help me.
Here was my code:
<TextBlock x:Name="webviewtest" Grid.Row="1" Text="This is a select-flyout test." FontSize="300" IsTextSelectionEnabled="true" >
<TextBlock.SelectionFlyout>
<Flyout>
<TextBlock Text="this is the flyout"></TextBlock>
</Flyout>
</TextBlock.SelectionFlyout>
</TextBlock>
And when I selected text, the flyout never showed up. It's obviously that I have been using it wrong. So I checked the Microsoft Docs and it said
Gets or sets the flyout that is shown when text is selected, or null if no flyout is shown.
And I can't find any samples about this online.
This is achievable by setting the TextBlock IsTextSelectionEnabled to True and by using a MenuFlyout to display the selected text.
XAML
<TextBlock x:Name="webviewtest" Text="This is a select-flyout test." FontSize="100" IsTextSelectionEnabled="True" RightTapped="webviewtest_RightTapped">
<FlyoutBase.AttachedFlyout>
<MenuFlyout x:Name="Flyout">
<MenuFlyout.Items>
<MenuFlyoutItem x:Name="FlyItem" Text="">
</MenuFlyoutItem>
</MenuFlyout.Items>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
</TextBlock>
C#
private void webviewtest_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
TextBlock tb = sender as TextBlock;
if (tb.SelectedText.Length > 0)
{
Item.Text = tb.SelectedText;
}
// Show at cursor position
Flyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement));
}
You need to use RichTextBlock to replace TextBlock and the platform is 17134 and laster.
<RichTextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
IsTextSelectionEnabled="True">
<RichTextBlock.ContextFlyout>
<Flyout>
<TextBlock Text="flyout" />
</Flyout>
</RichTextBlock.ContextFlyout>
<RichTextBlock.SelectionFlyout>
<Flyout>
<TextBlock Text="this is the flyout" />
</Flyout>
</RichTextBlock.SelectionFlyout>
<Paragraph>
welcome to blog.lindexi.com that has many blogs
</Paragraph>
</RichTextBlock>
The SelectionFlyout is work in touch. TextBlock.SelectionFlyout doesn't work · Issue #452 · Microsoft/microsoft-ui-xaml
All the code in github
I am trying to set the text for the controls inside the pivot item in the following way.
<Pivot x:Name="galleryPivot"
HeaderTemplate="{StaticResource headerTemplate}"
ItemTemplate="{StaticResource pivotTemplate}"
Margin="0,-10,0,10" SelectionChanged="galleryPivot_SelectionChanged">
</Pivot>
<Page.Resources>
<DataTemplate x:Key="pivotTemplate">
<Grid>
<TextBlock x:Name="one"></TextBlock>
</Grid>
</DataTemplate>
</Page.Resources>
I want to set the text for the text block namely "one" in the back end.
In the c# end I am doing in the following way.
PivotItem item = (sender as Pivot).ContainerFromItem((sender as Pivot).SelectedItem) as PivotItem;
var rootGrid = item.ContentTemplate.LoadContent() as Grid;
var tb = rootGrid.FindName("one") as TextBlock;
tb.Text = "test";
I am able to get the text block controls and properties of the text block, but when I am trying to set the text in the back end I am not getting anything in the front end where as if I set the property in xaml it is working absolutely fine.
What am I doing wrong?
In my code I am creating a new Label and formatting it and making it a child of a Stacked Panel. But inside of the Label I need to add a Text Block and I am having trouble to find out how to do this.
Using Code only I need to the WPF created by the code to work like this:
<Label Background="#000000" Foreground="#FFFFFF">
<TextBlock TextWrapping="Wrap" Text="Text Here"/>
</Label>
But I can not figure out how to get that TextBlock inside of the Label in the code, what I thought would work isn't working because it is a Label:
NewLabel.Children.Add(NewTextBlock);
But this works when I use it to add the NewLabel to the StackedPanel.
The whole reason I need this to work is because I need text wrapping in the Label, but can't use just the TextBlock or other Control.
Just set the TextBlock as the content of the label to achieve your requirement.
Label lbl = new Label ();
TextBlock txtBlock = new TextBlock ();
txtBlock.TextWrapping = TextWrapping.Wrap;
lbl.Content = txtBlock;
Also you could add a style for "Label" to your Resources which contains a default DataTemplate for type string. So all string content in any Label is wrapped.
<Application.Resources>
<Style TargetType="Label">
<Style.Resources>
<DataTemplate xmlns:sys="clr-namespace:System;assembly=mscorlib" DataType="{x:Type sys:String}">
<TextBlock TextWrapping="Wrap" Text="{Binding}" />
</DataTemplate>
</Style.Resources>
</Style>
</Application.Resources>
Then all you Need to do is set the string Content.
<Label Content="A very long string for my Label" />
or
var Label = new Label { Content = "A very long string for my Label" }
I'm binding a text box to a textblock, but it is not updated when I paste something using the context menu.
Following there is the XAML code for element binding:
<uc:CustomTextBox x:Name="txtBoxLastName"
Grid.Row="3"
Grid.Column="1"
Width="80"
Height="25"
HorizontalAlignment="Left" />
<TextBlock Grid.Row="4"
Grid.Column="1"
Width="100"
Height="100"
Text="{Binding Text,
ElementName=txtBoxLastName}" />
Context menu paste code:
this.SelectedText = Clipboard.GetText();
What's wrong with this code? Is there any other way to do the same ?
Regards.
Using the common Silverlight controls, the text pasted in the TextBox control is automatically pasted also in the TextBlock control.
I think the problem is in the code you're using to paste the text stored in the Clipboard, because you're setting the property SelectedText, when instead your TextBlock's Text property is bound to the Text property of the TextBox.
You can change the line from:
this.SelectedText = Clipboard.GetText();
to:
this.Text = Clipboard.GetText();
or, as second option, change the binding in your textblock from this:
Text="{Binding Text, ElementName=txtBoxLastName}"
to this:
Text="{Binding SelectedText, ElementName=txtBoxLastName}"
I'm trying to create a simple header template for an accordion object in silverlight 4.
I've added an image and a TextBlock to the header template of the AccordionItem. I want to hide or show the image dependant on the values entered on the page.
Because i want to bind these values directly to the actual accordion item, I've created a new type 'AccordionItemWithIcons' that simply inherits from AccordionItem but adds a couple of dependancy properties to handle this. I'm only showing a couple of those properties for brevity. :)
So, here's my accordion with my 'AccordionItemWithIcons' control. Note that the property 'CheckIsVisible' is of type 'Visibility'
<Grid x:Name="LayoutRoot">
<Controls:Accordion Height="100">
<my:AccordionItemWithIcons
x:Name="FirstItem"
Content="Content Text"
Header="Header Text"
CheckIsVisible="Collapsed"
EventSummary="Summary Text"
HeaderTemplate="{StaticResource AccordionItemHeaderTemplate1}"/>
</Controls:Accordion>
</Grid>
And here is the header template.
<DataTemplate x:Key="AccordionWithIcons_HeaderTemplate1" >
<Grid >
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<TextBlock Text="{Binding EventSummary}" />
<Image Visibility="{Binding CheckIsVisible}" Source="/Labyrinth;component/cross.png"/>
</StackPanel>
</Grid>
</DataTemplate>
Can anyone explain how I can bind the TextBlock's text and the Image's Visibility to the values set in the underlying AccordionItemWithIcons object? I've spent hours messing about with different DataContext's and sources and cannot seem to get this to work!
I don't know if helps to explain what I'm trying to achieve, but ultimately in the code behind i want to be able to say something like (shown below), to show or hide the icon in the header template.
FirstItem.CheckIsVisible = Visibility.Visible
For this, there exists a VisibilityToBooleanConverter
<BooleanToVisibilityConverter x:Key=”boolVisConverter”/>
[...]
Visibility="{Binding ElementName=anyCheckbox,
Path=IsChecked,
Converter={StaticResource boolVisConverter}}"