0

From trial and error I managed to highlight part of text in a textblock which is in a datatemplate of a listbox bounded to a property of a custom class. But the problem now is that when highlighting the text i get a weird unknown space between the highlighted text and the rest of the text.

Here is part of the XAML

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBox Name="textBox1" TextChanged="textBox1_TextChanged"/>
    <ListBox Grid.Row="1" Name="listBox1">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Name="gridOfListbox" Height="25" Margin="0,2">
                    <DockPanel Name="dockpanelWithTxtBlock">
                        <TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
                            <Run Background="Yellow" Text=""></Run>
                            <Run Text="{Binding ProductID}"></Run>
                        </TextBlock>
                    </DockPanel>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

And here part of the code used

ObservableCollection<TItem> items = new ObservableCollection<TItem>();
TItem[] source = new TItem[] { new TItem("Hello"), new TItem("World"), new TItem("System"), new TItem("SystemDefault"), new TItem("SystemFolder") };

And the method for event changedtext

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        string match = textBox1.Text;
        foreach (TItem TItem in listBox1.Items)
        {
            ListBoxItem lbi = (ListBoxItem)this.listBox1.ItemContainerGenerator.ContainerFromItem(TItem);
            TextBlock txtBlck = FindFirstElementInVisualTree<TextBlock>(lbi);
            Run bold = (Run)txtBlck.Inlines.FirstInline;
            Run normal = (Run)txtBlck.Inlines.LastInline;

            string s = bold.Text + normal.Text;

            if (s.ToLower().StartsWith(match.ToLower()))
            {
                bold.Text = s.Substring(0, match.Length);
                normal.Text = s.Substring(match.Length);
            }
            else
            {
                bold.Text = "";
                normal.Text = s;
            }
        }
    }

FindFirstElementInVisualTree method is used to find the textboxes needed to search of.

If anymore code is needed let me know. I also added an image to demonstrate what the problem is. An help will be appreciated! Link for image: https://i.stack.imgur.com/rOj0m.png

Merv
  • 1,039
  • 2
  • 12
  • 22

1 Answers1

1

When you use Run within a TextBlock in XAML, everything not wrapped in <> are considered actual strings. Having a line break would mean a space. Put the two Runs within the same line (without a space in between too).

<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
    <Run Background="Yellow" Text="" /><Run Text="{Binding ProductID}" />
</TextBlock>

Edit

By the way, I just saw your first question which was marked as duplicate. This question is asked correctly; so you should ask questions in this manner in the future.

Community
  • 1
  • 1
Jai
  • 8,165
  • 2
  • 21
  • 52