I work in WPF project and I'm new in this area.
I need to add link programatically.
Before and after the link I want to add some words.
After the words before link I put linebreak.
But the words after the link need to not have line break but continue in the same row.
But I got linebreak after each element that I add by 'inlines.add'.
Any solution?
Here is my code:
TextBlock tb = new TextBlock();
tb.Inlines.Add("this is my prefix
");
Hyperlink h = new Hyperlink();
h.NavigateUri = new System.Uri(helpLinkURL);
h.Inlines.Add("here is link text");
h.RequestNavigate += Hyperlink_RequestNavigate;
tb.Inlines.Add(h);
tb.Inlines.Add("this is my suffix");
helpLinkPlaceHolder.Inlines.Add(tb);
I get:
this is my prefix
here is link text
this is my suffix
And this is what I want to get:
this is my prefix
here is link text this is my suffix
Try to use Run instead of TextBlock. It has propierties and functions that allows you to do this.
This way you can do it in code behind:
var hyperlink = new Hyperlink(new Run("here is link text"))
{
NavigateUri = new Uri("https://google.com")
};
hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
var textblock = new TextBlock
{
Inlines =
{
new Run("this is my prefix"),
new LineBreak(),
hyperlink,
new Run(" this is my suffix")
}
};
placeholderContentControl.Content = textblock;
placeholderContentControl is of type <ContentControl>
How to do it in XAML:
<Grid>
<TextBlock Grid.Row="0">
this is my prefix
<LineBreak />
<Hyperlink NavigateUri="https://google.com" RequestNavigate="Hyperlink_RequestNavigate">
here is link text
</Hyperlink>
this is my suffix
</TextBlock>
</Grid>
both result in the requested result: image
Related
I have winform hosting WPF control text block where the contents of text block is dynamic. I want to set the height of the text block based on content. mean while text block is hosted in windows usercontrol (TitleBarHost) and in turn this windows control is dynamically added in another winform at run time. Below is piece of code where control is added dynamically.
Control titleBar = new TitleBarHost(titleInfo);
(titleBar as ICustomUI).Initialize(_application, extraData);
panelHeader.Controls.Add(titleBar);
titleBar.Dock = DockStyle.Top;
TitleBarView.xaml
<Grid>
<TextBlock x:Name="txtTitleBar" Text="{Binding Text}" Style="{StaticResource TextBlockStyle}"/>
</Grid>
TitleBarHost.cs
public void Initialize(object application, object configurationData)
{
m_Model = new TitleBarModel(this.titleInfo, application, configurationData);
m_ViewModel = new TitleBarViewModel(m_Model);
m_View = new TitleBarView { DataContext = m_ViewModel };
if (m_ViewModel != null)
{
m_View.FormatString(m_ViewModel.Text);
}
elementHost1.Child = m_View;
}
I need to set the text block height dynamic based on content. I tried to use the ActualHeight prop of text block but doesn't work. Also tried using height Auto, doesn't works!
I know how to use an image from my assembly resources in XAML for the Image element:
<Image Source="pack://application:,,,/MyImage.png" />
But, how can I do it for the Text attribute of a TextBox? Something similar to:
<TextBox Text="pack://application:,,,/MyTextFile.txt" />
Here is my current solution, but requires some code-behind:
Uri licenseUri = new Uri("pack://application:,,,/MyTextFile.txt", UriKind.Absolute);
StreamResourceInfo info = Application.GetResourceStream(licenseUri);
StreamReader reader = new StreamReader(info.Stream);
_textBox.Text = reader.ReadToEnd();
I have a pivot control:
PivotItem sectionPivot = new PivotItem()
{
Header = sect.Name,
Content = new StackPanel()
};
How can I add content to it?
Edit: The reason I'm doing it programmatically is because I don't know how many pivots there will be or what they will contain until runtime. Is there some way to do that in XAML?
If you're going to do it all programatically, just add stuff to the stack panel you just created.
var panel = new StackPanel();
panel.Children.Add(new TextBlock() { Text = "Hello" });
PivotItem sectionPivot = new PivotItem()
{
Header = sect.Name,
Content = panel;
};
I typed all that without doing any checking, but hypothetically that should work...
Another answer from me. The OP added info to the question that they don't know how many there could be, and if you could still do it in XAML.
Yes, you can.
The Pivot control has an ItemsSource property, and you could bind that to something in your class that is being populated dynamically.
<controls:Pivot Title="MY APPLICATION" ItemsSource="{Binding MyPivotItemsSource}" />
Each item in that source would end up as a pivotitem. You'd also have to set up templates and stuff, so its still a lot of work...
hello community any one can please say how to add controls dynamically into a Stack Panel
note: what i need is i have to create a menu which get data from database and creates menu items accordingly can any one say how can i create such menus i am new to silver light
I am using silverlight 3 beta and expression blend3 + sketch flow please help me to know how to design those
Excuse the variable names, but here is a code snippet of dynamically adding items to a stack panel
StackPanel split = new StackPanel();
TextBlock expected = new TextBlock();
expected.Text = "Expected Final Bonus";
TextBlock meh = new TextBlock();
meh.Text = Math.Round(((QuoteData)results.First()).ExpectedBonus * 100, 2) + "%";
split.Children.Add(expected);
split.Children.Add(meh);
TextBlock disc = new TextBlock();
disc.Text = "Discretionary Percentage";
TextBlock number = new TextBlock();
number.Text = Math.Round(((QuoteData)results.First()).Discretionary * 100, 2) + "%";
split.Children.Add(disc);
split.Children.Add(number);
Here you can see that I also created the stack panel dynamically, however, you can also create it using XAML.
Something like this should work:
<StackPanel Grid.Row="3" Grid.Column="1" Name="split" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto">
First, if your StackPanel is already in your XAML, handle the Loaded event:
<StackPanel x:Name="spValue" Loaded="spValue_Loaded">
</StackPanel>
private void spValue_Loaded(object sender, RoutedEventArgs e)
{
StackPanel stackPanel = (sender as StackPanel);
stackPanel.Children.Clear();
stackPanel.Children.Add(XamlReader.Load(XElement.Parse(xaml).ToString()) as FrameworkElement);
}
The controls are created with XAMLReader from your stuff loaded from the DB. You can adapt all that to your particular scenario (menu and menu items...)
I have a window with a title on it. When the user selects a choice from a drop down list, the title image can change. The problem is when the image loads, it's a blurred, stretched, and pixelated. These are PNG files I'm working with and they look good prior to setting the source dynamically.
Here's the code I'm using to change the image's source.
string strUri2 = String.Format(#"pack://application:,,,/MyAssembly;component/resources/main titles/{0}", CurrenSelection.TitleImage);
Stream iconStream2 = App.GetResourceStream(new Uri(strUri2)).Stream;
imgTitle.Source = HelperFunctions.returnImage(iconStream2);
Here are the helper functions.
public static BitmapImage returnImage(Stream iconStream)
{
Bitmap brush = new Bitmap(iconStream);
System.Drawing.Image img = brush.GetThumbnailImage(brush.Height, brush.Width, null, System.IntPtr.Zero);
var imgbrush = new BitmapImage();
imgbrush.BeginInit();
imgbrush.StreamSource = ConvertImageToMemoryStream(img);
imgbrush.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
imgbrush.EndInit();
var ib = new ImageBrush(imgbrush);
return imgbrush;
}
public static MemoryStream ConvertImageToMemoryStream(System.Drawing.Image img)
{
var ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms;
}
And the XAML
<Image x:Name="imgTitle" HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="1" Stretch="None" d:IsLocked="False"/>
And for Ref:
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Anyone have any ideas what's up?
I can think of two things:
First, try loading the image with:
string strUri2 = String.Format(#"pack://application:,,,/MyAseemby;component/resources/main titles/{0}", CurrenSelection.TitleImage);
imgTitle.Source = new BitmapImage(new Uri(strUri2));
Maybe the problem is with WinForm's image resizing, if the image is stretched set Stretch on the image control to "Uniform" or "UnfirofmToFill".
Second option is that maybe the image is not aligned to the pixel grid, you can read about it on my blog at http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx
Hey, this one is kind of ugly but it's one line only:
imgTitle.Source = new BitmapImage(new Uri(#"pack://application:,,,/YourAssembly;component/your_image.png"));
Here is how it worked beautifully for me.
In the window resources add the image.
<Image x:Key="delImg" >
<Image.Source>
<BitmapImage UriSource="Images/delitem.gif"></BitmapImage>
</Image.Source>
</Image>
Then the code goes like this.
Image img = new Image()
img.Source = ((Image)this.Resources["delImg"]).Source;
"this" is referring to the Window object
Like for me -> working is:
string strUri2 = Directory.GetCurrentDirectory()+#"/Images/ok_progress.png";
image1.Source = new BitmapImage(new Uri(strUri2));
Me.imgAddNew.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/SPMS;component/Images/Cancel__Red-64.png", UriKind.Relative))
Try Stretch="UniformToFill" on the Image