How can I create a hyperlink in C# code instead of XAML - c#

How can I create a hyper link in C# code that looks like the following in XAML?:
<TextBlock>
<Hyperlink Click="HyperLinkClick">New Hyperlink</Hyperlink>
</TextBlock>

MSDN usually has very good examples. Combining the examples for TextBlock and Hyperlink:
TextBlock textBlock1 = new TextBlock();
Run run3 = new Run("Link Text.");
Hyperlink hyperl = new Hyperlink(run3);
hyperl.NavigateUri = new Uri("http://search.msn.com");
textBlock1.Inlines.Add(hyperl);

Related

Adding image to a button in WPF C#, image not appearing

I want to create a button in my windows 8 desktop app, which will have an image and a text block. I have to do this using C# coding.
My code is as follows,
Button btn = new Button();
StackPanel btnContentPanel = new StackPanel();
btnContentPanel.Orientation = Orientation.Horizontal;
Image img = new Image();
img.Source = new BitmapImage(newUri(#"C:\Users\Desktop\Images\download.jpg"));
img.Stretch = Stretch.Uniform;
btnContentPanel.Children.Add(img);
TextBlock txBlock = new TextBlock();
txBlock.Text = "My Button";
btnContentPanel.Children.Add(txBlock);
btn.Content = btnContentPanel;
This is not giving any error but the image is not getting displayed. If I add another text block in place of the image, then its appearing, but not the image.
Am I missing anything ? Please help, thank you.
Try building your button like this:
Button btn= new Button
{
Width = 30,
Height = 30,
Content = new Image
{
Source = new BitmapImage(#"C:\Users\Desktop\Images\download.jpg"))
}
};
In the case of a 'missing' image there are several things to consider:
When Xaml can't locate a resource it might ignore it (when it won't throw a XamlParseException)
The Resource must be properly added and defined:
make sure it exists in your project where expected.
Make sure it is built with your project as a Resource.
(Right click -> Properties -> BuildAction='Resource')
Another thing to try in similar cases, which is also useful for reusing of the image (or any other resource):
Define your Image as a Resource in your Xaml:
<UserCondrol.Resources>
<Image x:Key="MyImage" Source.../>
</UserControl.Resources>
And later use it in your desired control/controls:
<Button Content={StaticResource MyImage} />

How to extract the UI element from data template?

I used the following code to create data template items:
FrameworkElementFactory textCtrl = new FrameworkElementFactory(typeof(TextBox));
Binding binding1 = new Binding();
string path = "Syncfusion";
binding1.Path = new PropertyPath(path);
textCtrl.SetValue(TextBox.TextProperty,binding1);
Binding binding2 = new Binding();
string path2 = "Text1";
binding2.Path = new PropertyPath(path2);
textCtrl.SetValue(TextBox.NameProperty, binding2);
DataTemplate dt = new DataTemplate();
dt.VisualTree = textCtrl;
How to get the text box from the data template..?
I tried the following links
Link 1
Link 2
But I did not get the things correctly...
I used the below code in xaml
<DataTemplate>
<TextBlock Text="{Binding CellBoundValue}" gridcommon:VisualContainer.WantsMouseInput="False"/>
</DataTemplate>
Can anyone help me on this?
As far as I know, Microsoft does not recommend to use the FrameworkElementFactory, it may get deprecated some times (not sure about this).
But if you want to do it anyway, you must apply your DataTemplate to create instances of controls declared in the DataTemplate. You can do this for example with a ContentControl or an ItemsControl.

How to set HeaderTemplate Property of HeaderedContentControl Programmatically

I am working in WPF and I need to set the Text and ToolTip of a Header of a HeaderedContentControl. So what I am trying to do is to create a template as below:
System.Windows.DataTemplate template = new System.Windows.DataTemplate();
template.DataType = typeof(HeaderedContentControl);
System.Windows.FrameworkElementFactory blockFactory = new System.Windows.FrameworkElementFactory(typeof(TextBlock));
blockFactory.SetValue(TextBlock.TextProperty, "The Header Text");
blockFactory.SetValue(TextBlock.ToolTipProperty, "The ToolTip");
template.VisualTree = blockFactory;
myHeaderedContentControl.HeaderTemplate = template;
But when I run the program the header is displayed empty. What am I doing wrong?
Hope someone can help, Thank you in advance
Have no idea why do you use template in a such way. Why not just set header property with the text block?
myHeaderedContentControl.Header = new TextBlock
{
Text = "Some text",
ToolTip = "Some tooltip"
};
Moreover, it is even better idea to define all that in XAML:
<HeaderedContentControl x:Name="control">
<HeaderedContentControl.Header>
<TextBlock Text="Some text" ToolTip="Some tooltip"/>
</HeaderedContentControl.Header>
</HeaderedContentControl>

How to assign a dynamic resource style in code?

I want to produce in code the equivalent of this in XAML:
<TextBlock
Text="Title:"
Width="{Binding FormLabelColumnWidth}"
Style="{DynamicResource FormLabelStyle}"/>
I can do the text and the width, but how do I assign the dynamic resource to the style:
TextBlock tb = new TextBlock();
tb.Text = "Title:";
tb.Width = FormLabelColumnWidth;
tb.Style = ???
You should use FrameworkElement.SetResourceReference if you want true DynamicResource behaviour - ie updating of the target element when the resource changes.
tb.SetResourceReference(Control.StyleProperty, "FormLabelStyle")
You can try:
tb.Style = (Style)FindResource("FormLabelStyle");
Enjoy!
The original question was how to make it Dynamic, which means if the resource changes the control will update. The best answer above used SetResourceReference. For the Xamarin framework this is not available but SetDynamicResource is and it does exactly what the original poster was asking. Simple example
Label title = new Label();
title.Text = "Title";
title.SetDynamicResource(Label.TextColorProperty, "textColor");
title.SetDynamicResource(Label.BackgroundColorProperty, "backgroundColor");
Now calling:
App.Current.Resources["textColor"] = Color.AliceBlue;
App.Current.Resources["backgroundColor"] = Color.BlueViolet;
Causes the properties to change for all controls using the resource this way. This should work for any property.
This should work:
tb.SetValue(Control.StyleProperty, "FormLabelStyle");
Application.Current.Resources.TryGetValue("ResourceKey", out var value)

Import XAML into WPF RichTextBox

I have a WPF RichTextBox that is dynamically built in a WPF web service. This web service accepts a xaml string that is extracted from the contents of a third-party silverlight RichTextBox control.
<Paragraph TextAlignment=\"Left\"><Run FontFamily=\"Comic Sans MS\" FontSize=\"16\" Foreground=\"#FF0000FF\" FontWeight=\"Bold\" >This text is blue and bold.</Run></Paragraph>
How do I insert this xaml into my WPF RichTextBox? I somewhat understand the concepts of the FlowDocument and Paragraph and Run so I can populate the WPF RichTextBox with text using the code below,
FlowDocument flowDocument = new FlowDocument();
Paragraph par = new Paragraph();
par.FontSize = 16;
par.FontWeight = FontWeights.Bold;
par.Inlines.Add(new Run("Paragraph text"));
flowDocument.Blocks.Add(par);
rtb.Document = flowDocument;
But what I really don't want to have to parse through the xaml myself to build a paragraph as it can get very complicated. Is there a way to just have the control know how to parse the passed in xaml?
You can use XamlReader to read your Xaml string and convert it to a control:
string templateString = "<Paragraph xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TextAlignment=\"Left\"><Run FontFamily=\"Comic Sans MS\" FontSize=\"16\" Foreground=\"#FF0000FF\" FontWeight=\"Bold\" >This text is blue and bold.</Run></Paragraph>";
StringReader stringReader = new StringReader(templateString);
XmlReader xmlReader = XmlReader.Create(stringReader);
Paragraph template = (Paragraph)XamlReader.Load(xmlReader);
Just make sure you include the following tag in your template:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
HTH

Categories

Resources