I am creating a hyperlink button like this:
HyperlinkButton hlbMail = new HyperlinkButton();
hlbMail.Height = 89;
hlbMail.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
hlbMail.VerticalAlignment = System.Windows.VerticalAlignment.Top;
hlbMail.Margin = new Thickness(60, -70, 0, 0);
hlbMail.Width = 290;
hlbMail.FontSize = 22;
TextBlock btnContent = new TextBlock();
btnContent.TextWrapping = TextWrapping.Wrap;
btnContent.Text = message;
btnContent.Visibility = System.Windows.Visibility.Visible;
hlbMail.Content = btnContent;
hlbMail.Visibility = System.Windows.Visibility.Visible;
mailStackPanel.Children.Add(hlbMail);
mailscrollViewer.Content = mailStackPanel;
PIMail.Content = mailscrollViewer;
But i have a problem in which the content of the button is not being displayed. As can be seen, the content is supposed to be the text of the TextBlock (the message var is a string which is not empty). What can be the reason that the content is not being shown?
You need to use this:
hlbMail.Content= btnContent.Text;
I think you forgot to add the .Text.
You cannot have a hyperlinkbutton having textblock as its content. But you can have the text of a textblock as the content of the hyperlink
As you also mentioned you need the text of textblock than you need to specify that you need the text by adding the btnContent.Text;
Related
I have a ListbBox that contains grid items. Inside grids are TextBlocks. How can I resolve the textblock.Text of each listbox item?
I think that listbox.Items[index-1] returns an object but I can't get the textblock value.
I create dynamically
Grid g = new Grid();
g.Background = new SolidColorBrush(Colors.Brown);
And then
TextBlock txt = new TextBlock();
txt.Text = "some dynamically text";
And
g.Children.Add(txt);
listbox.Items.Add(g);
where listbox is my ListBox
When I load some values I want to resolve the text
int epilogi = listbox.SelectedIndex;
listbox.Items.RemoveAt(epilogi);
object tempkati = listbox.Items[epilogi-1];
I want the text from the tempkati object
Try this:
var grid = listbox.Items[epilogi - 1] as Grid;
var textblock = grid.Children[0] as TextBlock;
var text = textblock.Text;
I have created a RichTextBlock like this
RichTextBlock RTB = new RichTextBlock();
But I can not find a way to add text and URL's to the RichTextBlock.
I am thinking, if I need a specific using or something similiar
try this :
// creat your RichTextBlock
RichTextBlock MyRTB = new RichTextBlock();
// Create a Run of plain text and some bold text.
Run myRun1 = new Run();
myRun1.Text = "Some text here";
myRun1.FontStyle = FontStyle.Oblique;
myRun1.FontSize = 72;
Run myRun2 = new Run();
myRun2.Text = "Other text";
myRun2.FontSize = 140;
myRun2.Foreground = new SolidColorBrush(Colors.Red);
// Create a paragraph and add the Run and Bold to it.
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun1);
myParagraph.Inlines.Add(myRun2);
// add paragraphe to your RichTextBlock blocks
MyRTB.Blocks.Add(myParagraph);
// now, add RichTextBlock to the grid (or any other controler in your page)
ParentGrind.Children.Add(MyRTB);
// update layoute
ParentGrind.UpdateLayout();
this work well for me to add and format text dynamically
I define my ApplicationBar in code like that:
private void BuildApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar();
ApplicationBar.Opacity = 0.8;
ApplicationBar.ForegroundColor = Color.FromArgb(0, 138, 204, 34);
// Create a new button and set the text value to the localized string from AppResources.
ApplicationBarIconButton CheckInExitAppBarButton = new ApplicationBarIconButton(new Uri("icons/check_in.png", UriKind.Relative));
CheckInExitAppBarButton.Text = AppResource.CheckInExit;
ApplicationBar.Buttons.Add(CheckInExitAppBarButton);
CheckInExitAppBarButton.Click += new EventHandler(CheckInExitAppBarButton_Click);
}
Atfer that I can see that color of icons changed, but I can't see text under them. When I do this without ApplicationBar.Color I can see both icon + text but in white which doesn't interest me
The text uses the alpha value of the foreground color, and you're setting it to 0 (transparent). Set it to 255 instead and it'll work:
private void BuildApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar();
ApplicationBar.Opacity = 0.8;
ApplicationBar.ForegroundColor = Color.FromArgb(255, 138, 204, 34);
// Create a new button and set the text value to the localized string from AppResources.
ApplicationBarIconButton CheckInExitAppBarButton = new ApplicationBarIconButton(new Uri("icons/check_in.png", UriKind.Relative));
CheckInExitAppBarButton.Text = AppResource.CheckInExit;
ApplicationBar.Buttons.Add(CheckInExitAppBarButton);
CheckInExitAppBarButton.Click += new EventHandler(CheckInExitAppBarButton_Click);
}
I was trying to create two different kinds of buttons: buttons with background and buttons without background.
And unfortunately I can't figure out how I can set some buttons to use the first style and some buttons to use the second.
Here's a code sample (although i doubt it'll help)
Is there some way to change the Style of the button in the .cs file, or any workaround?
//init hyperlink
Button hyperlink = new Button();
Label hyperlink_label = new Label();
hyperlink_label.Text = hyperlink_text;
hyperlink_label.ModifyFg(StateType.Normal, new Gdk.Color(0,0,0));
hyperlink_label.ModifyFg(StateType.Prelight, new Gdk.Color(255,255,255));
hyperlink_label.ModifyFg(StateType.Selected, new Gdk.Color(255,255,255));
Pango.FontDescription hyperlinkFontDesc = new Pango.FontDescription();
hyperlinkFontDesc.Family = "Adobe Garamond Pro";
hyperlinkFontDesc.AbsoluteSize = hyperlink_fontSize * Pango.Scale.PangoScale;
hyperlink_label.ModifyFont(hyperlinkFontDesc);
hyperlink.Add(hyperlink_label);
mainWindowFixed.Put(hyperlink, hyperlink_pos[0], hyperlink_pos[1]);
//init startbutton
Button startButton = new Button();
Label startLabel = new Label();
startLabel.Text = startButton_text;
startButton.Settings.ThemeName = "Ludwig_AutoUpdater";
startLabel.ModifyFg(StateType.Normal, new Gdk.Color(255,255,255));
startLabel.ModifyFg(StateType.Prelight, new Gdk.Color(255,255,255));
startLabel.ModifyFg(StateType.Selected, new Gdk.Color(255,255,255));
Pango.FontDescription startLabelFontDesc = new Pango.FontDescription();
startLabelFontDesc.Family = "Klavika bd";
startLabelFontDesc.AbsoluteSize = startButton_fontSize * Pango.Scale.PangoScale;
startButton.Add(startLabel);
startButton.Child.ModifyFont(startLabelFontDesc);
startButton.SetSizeRequest(startButton_size[0], startButton_size[1]);
mainWindowFixed.Put(startButton, startButton_pos[0],startButton_pos[1]);
Don't change the style of Gtk.Button. Create a custom widget derived from Gtk.Button and change that widget's style. For example:
public class BGButton : Gtk.Button {}
and
public class NoBGButton : Gtk.Button {}
and now change the style of BGButton and NoBGButton.
Then if you want a Button with a Background use BGButton , else use NoBGButton.
I'm loading elements on a page dynamically (reading the contents of an XML file). The dynamic content is loaded into a StackPanel. Each element of the content consists of a TextBlock and one other UI element, so for each pair I create a new StackPanel which is then added to the parent StackPanel. The code looks like this:
TextBlock header = new TextBlock() {
Text = "Heading 1",
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"],
};
TextBox item = new TextBox() {
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Top,
};
StackPanel sp = new StackPanel();
sp.Children.Add( header );
sp.Children.Add( item );
parentSP.Children.Add( sp );
I want to add a ContextMenu to this StackPanel (sp, not parentSP); depending on some parameters read from the file it could be one of 2 different context menus. I tried the following but it is not working:
ContextMenu cm = new ContextMenu();
RoutedEventHandler clickHandler = new RoutedEventHandler( OnContextMenuClicked );
// Add "edit" entry
MenuItem menuItem = new MenuItem() {
Header = "edit",
Tag = "edit",
};
menuItem.Click += clickHandler;
cm.Items.Add( menuItem );
// Add "delete" entry
menuItem = new MenuItem() {
Header = "delete",
Tag = "delete",
};
menuItem.Click += clickHandler;
cm.Items.Add( menuItem );
parentSP.Children.Add( cm );
How do I add a context menu to the StackPanel programmatically?
Also, is there a better way to solve this problem? Maybe by storing the 2 different types of context menus in a XAML resources section and adding them as needed? I tried doing this by adding the context menus to the parent's StackPanel.Resource section but got an error saying "A property element cannot be the direct child of another property element"
Thanks in advance for your help
ContextMenuService.SetContextMenu(sp, cm);