1

If it helps to know, I'm using Caliburn.Micro, and have laid out everything based on the MVVM framework requirements as I understand them.

Here is the relevant XAML ...

  <ListView ItemsSource="{Binding ProductListBox}">
     <ListView.ItemTemplate>
         <DataTemplate>
             <StackPanel>
                 <TextBlock Text="{Binding ProductID}"/>
                 <TextBlock Text="{Binding ProductDescription}"/>
                 <TextBlock Text="{Binding ProductDescriptionExtended}"/>
             </StackPanel>
         </DataTemplate>
     </ListView.ItemTemplate>
  </ListView>

The above content of the ListView is created dynamically based on the content of ProductListBox, which sits inside this class...

public class MainViewModel : Screen
{
    public List<ProductModel> ProductListBox { get; private set; }

    public void GetProductsButton()
    {
        DBAccess db = new DBAccess();
        ProductListBox = db.GetProducts(SearchTextBox);
        NotifyOfPropertyChange(() => ProductListBox);

    }

}

That List is populated when a Button is clicked, it causes the above GetProductsButton() method to start.

Over in the DBAccess class, in the GetProducts method I would like to change the format of the text in the ProductListBox<>.ProductDescription. Its a string, but I'm happy to change it to any type should it help the cause!

The kind of change I would like to achieve is simply (highlighting) changing the background color of selected text based on found search terms the user had typed in, that the bound XAML TextBlock will then display.

What I cant work out is how to highlight any text at this time via C#, that will then be displayed purely by the bound XAML control... I've only been able to do it by hardcoding XAML which is not going to help for what I'm trying to achieve.

In the DBAccess class and inside the GetPeoducts method, this is the kind of thing I have tried in order to make this happen...

I've pasted in this code...

TextBlock textBlock1 = new TextBlock();
textBlock1.Inlines.Add(new Bold(new Run("TextBlock")));
textBlock1.Inlines.Add(new Run(" is designed to be "));
textBlock1.Inlines.Add(new Italic(new Run("lightweight")));
textBlock1.Inlines.Add(new Run(", and is geared specifically at integrating "));
textBlock1.Inlines.Add(new Italic(new Run("small")));
textBlock1.Inlines.Add(new Run(" portions of flow content into a UI."));

Which although doesn't change background color, will lead to that if I could make it work ;)

I think in every example of the kind of code I've quoted above, the writer always ends that code with something like this...

this.Content = textBlock1;

And the examples I've seen also always seem to inherit from the Window class. I don't want to do it this way. I want to find my search terms, highlight the given text and allow the bound TextBlock to update itself based on the populated List of type ProductModel.

I've tried changing the type of ProductDescription to a TextBlock, and used the above code to allow my XAML to remain bound directly to it

ie..

PM.ProductDescription = textBlock1;

In this case there was no GUI output at all for the content.

I've also tried straight HTML-like formatting to the string type

ie..

PM.ProductDescription = "<bold>Hello World</bold>";

This gives the literal text output of <bold>Hello World</bold> on the TextBlock control though.

I've also tried changing the type of Binding key from Text to other things in the hope I might work it out, without success.

Any ideas or help would be greatly appreciated, thank you!

Here is a little picture of what I would like to see the program be able to do..

text formatted with highlighted text

dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • Done, see link at bottom of question. thank you – Chris Palmer Sep 27 '18 at 14:57
  • Take a look on [this](https://stackoverflow.com/questions/751741/wpf-textblock-highlight-certain-parts-based-on-search-condition) question. There are lots of links to other questions and articles how people implement similar functionality. – dymanoid Sep 27 '18 at 15:04
  • Some good info there, and I think this answer has most merit. I like the idea of a converter. https://stackoverflow.com/a/22027054/8812893 But the problem remains, I'm in the same position.. I convert my text to what I want, either as a XAML string or control but the screen doesn't show anything new. It doesn't update Or perhaps more importantly maybe its not being assigned because I'm doing that incorrectly via the binding? – Chris Palmer Sep 29 '18 at 08:56
  • Do you understand what I mean? With Caliburn.Micro my C# code doesn't find a specific control and then modify it. XAML is setup to look at a property, and if I change the text, it changes on the screen as plain text. But try to do anything else, like background colors etc and nothing happens. – Chris Palmer Sep 29 '18 at 09:00

0 Answers0