Disclamer, this is my first post, so I don't know if I got everything I needed to in here. Please keep that in mind while going though my question.
I have been trying to create a part of my program that will be able to send hyperlinks in messages between users. As part of it, these hyperlinks are saved separately from the body of the message (which is just a string) with a starting position, length, and Uri. I figured out all of the parsing things, but what I cannot figure out is how to display the message with they hyperlinks clickable to the user.
I have tried using an ItemsControl, creating a class that has the text before the hyperlink and the hyperlink info and using something like:
<ItemsControl ItemsSource="{Binding Pairs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Pretext, Mode=OneWay}"/>
<Hyperlink NavigateUri="{Binding Hyperlink.Url, Mode=OneWay}">
<Run Text="{Binding Hyperlink.LinkText, Mode=OneWay}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
However, I cannot figure out how to make the items run together and not set as separate items, and I don't think that that is possible.
I also have tried to, in the code behind, set a textbox's Inlines to each of the "sections" of the message, similar to this question, however, that requires to be running in the code-behind. I tried to add an InlineCollection to the message, but there is no where to set in in the .xaml file.
So my question is where do I go from here? How do I solve any of these issues? Is there a solution?
Thanks in advance
EDIT:
In case it is unclear, I am trying to display a message (type string) that has a list of hyperlinks (custom object with INT start position, INT length, and a URI). The number of these hyperlinks is not consistent, and I want the message to flow on a webpage.
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.
This is what my main program GUI will look like, what I am attempting to do, is to create a reminder application in C# using wpf.
I am going to use a scroll viewer which is going to be displaying the data to the user, namely the reminders they currently have.
Each time the user adds a reminder, it will add this:
What I am wanting to do is that, when ever the user add's a new reminder, there will be a new set of data added to the scrollviewer.
What would be the best way of achieving this?
Am I able to hold the xaml data and add it during execution?
Thanks for the help
What you want to do can be accomplished not by dynamic Xaml, but by the use of a templated control which can accept dynamic data. For example you wouldn't consider using a listbox for your labels because you are not showing the data in a list right?
But a listbox is just a conveyor belt for what you want to achive. Say you want more than a label, how about three labels. Via binding to a proper structure you can get what is needed.
Here is an example
<ListBox ItemsSource="{Binding myReminders }">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=ReminderName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Once that is bound to a list of my reminder data objects (which the list can dynamically change), we have the ability to show the reminders in any format we want. One just uses the above to style it appropriately. (Check out WPF's Templating Overview for a great example).
The use of templates is done in other controls, so if the listbox is not to your liking, look into other templated controls.
I have a list of mail I generate, and displayings some details into a dataGrid.
Some will works, some will be in errors.
I want to display, into the last datagrid column, a button, if the process successed, or an image if the process failed.
So, I got this with juste a button :
<sdk:DataGridTemplateColumn Header="Voir">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="btOpen"
Click="btOpen_Click"
IsEnabled="True"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
This button will open the document.
But as I said, some process can fail, so instead of a button, I must display an image.
Can I add an image into my datagridTemplateColumn, and then displying one of them depending on the success or fail of the process(easy to know)?
If I can't, how can I do what I want here? Can I do it without adding to different columns?
Thank you.
You can add the image alongside the Button and bind the visibility of each of them to the success / fail of the process
Try to create two different templates on xaml. One with the image, one with the button. Then on runtime you can change it according to your needs.
<Grid.Resources>
<DataTemplate x:key="ImageDT">
<Image>
</DataTemplate>
<DataTemplate x:key="ButtonTemplate">
<Button>
</DataTemplate>
</Grid.Resources>
Then on runtime, maybe on Load/LoadingRow event
//Here you get the row or column or cell you need
if(ProcessSuccess)
Grid.Columns["Voir"].CellTemplate = Grid.Resources["ImageDT"] ;
else
Grid.Columns["Voir"].CellTemplate = Grid.Resources["ButtonTemplate"];
Please note that i`m not giving you the right code and only the idea.
Hope it helps
I'm creating a menu from a ListBox. I'm using FontAwesome to create some font-icons. This is part of the ListBox ItemTemplate.
<TextBlock FontFamily="FontAwesome" VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="32" Text="{Binding MenuCode}"
ToolTip="{Binding Tooltip}" >
The problem resides within the TextBlock's Text. I need to display the symbol, not the menu code. So, for example, if I use Text="" directly, then the music icon appears (fixed for all the items), but when I use DataBinding (each item has a different symbol): Text="{Binding MenuCode}" then the text (that is, the menu code as string) appears (as text, no icon). I guess problem is related with encoding, but can't fix it. Any idea?
Wrong escape sequence. HTML uses '#&x' while C# uses \u. So your "#&xF001" would become "\uF001"
I am trying to build a 'comments control' (In a win8 XAML app, which is similar to Silverlight) which will load and render a comment tree for the user.
Each comment can have 0 or 1+ children comments (which recurses through each comment).
Each comment displays a set of info, including author, time, the comment itself, etc.
The approach I initially took to build this is to use a ListView which has a 'CommentItem' control binding.
<TextBlock Grid.Row="1" TextWrapping="Wrap" Text="{Binding commentText}" FontSize="11" FontFamily="Global User Interface" />
<ListView Grid.Row="2" x:Name="CommentRepliesList" ItemsSource="{Binding}" >
<ListView.ItemTemplate>
<DataTemplate>
<local:CommentItem Tag="{Binding}"></local:CommentItem>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The above will just recurse through each comment, apply the comment text, and then create a new commentitem for each comment child, recurse through again, etc.
The issue, however, is this is extremely slow and non-performant.
Does anyone know a more efficient way to do this? Is ListView the appropriate control to use for this?