0

Hi I want to highlight part of a text that is in a texblock and that textblock is in a datatemplate of a listbox. I'm trying to do this as the user is searching through a list from entering text in a textBOX. (It is something similar to when searching in Windows File explorer) The textblock is bound to a property of a custom class.

Here is the XAML of the listbox DataTemplate

<ListBox Grid.Row="1" HorizontalContentAlignment="Stretch" Name="
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Name="me" Height="25" Margin="0,2">
                <DockPanel>
                    <TextBlock DockPanel.Dock="Left" FontSize="15" TextAlignment="Center" Text="{Binding ProductID}"></TextBlock>
                    <Image HorizontalAlignment="Right" MouseUp="listboxStockPickItems_AddClick" DockPanel.Dock="Right" VerticalAlignment="Stretch" Margin="0,0,5,0" Cursor="Hand" Width="20" Height="20" Source="/Images/addItem.png"></Image>
                </DockPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Here is the custom class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace UCServer
{
[Serializable]
public class Articulo
{
    public int ID { get; set; }

    public string ProductID { get; set; }

    public string Name { get; set; }

    public int Amount { get; set; }

    public double Price { get; set; }
}
}

Any help will be greatly appreciated!

Merv
  • 1,039
  • 2
  • 12
  • 22
  • The marked duplicate provides extensive discussion on the question of highlighting portions of `TextBlock`, along with a number of different approaches to solve that goal. See also e.g. [Selectable TextBlock or a TextBox with formatted text in WPF](http://stackoverflow.com/q/2898576) and [How to highlight the word(s) with bold in text block for specified keyword using the attached property](http://stackoverflow.com/q/30658606). – Peter Duniho Jul 10 '16 at 18:58
  • If you are still having trouble after reviewing those references and trying to apply their advice, please post a new question in which you've included a good [mcve] that shows clearly what you've tried, along with a precise description of what specifically you are having trouble with. – Peter Duniho Jul 10 '16 at 18:58
  • Peter please read my question and explanation good before marking it as duplicate. The problem here is that I need to highlight part of a textblock that is in a listbox datatemplate that is bound to a property of a custom class, using databinding as you can see in the XAML I posted. I can surely do it with a predefined normal textblock and with Runs but I repeat, im talking about a textblock defined in a listbox datatemplate that is bound to a property of a custom made class. And moreover not a listbox where you click on the listboxItem but by using a textbox for input.Thank you. – Merv Jul 10 '16 at 23:05
  • _"please read my question and explanation"_ -- I did read it. The problem is that your question is horribly vague and there could be any number of things you need help with. I selected the duplicate post based on the most fundamental part of the question, as you made no indication that you know how to get highlighting to work in a `TextBlock` regardless of context. If you need help with something else, then (as I've already pointed out) you need to post a better question, with a good [mcve] and a specific problem description. – Peter Duniho Jul 10 '16 at 23:10
  • I really dont see what you dont understand the question is simple I want to highlight part of text in textblock which is in a listbox datatemplate and which is databounded to a property of a custom class while typing text in a textbox outside the listbox. Its that simple. I dont have any idea regarding highlighting text in a texblock thats why im asking. But I will try again in another question since I managed to do it on my own but still have a tiny problem. Thanks for ur help – Merv Jul 11 '16 at 00:10
  • @peter I made another Q here. If you can help it would be awesome. Q: [link] (Unknown space while highlighting part of text in a texblock (WPF) ). Thanks – Merv Jul 11 '16 at 00:27

0 Answers0