C # WPF ListView Individualize each row font size dynamically

I am trying to create a dynamic list box in WPF using ListView. My code is read in a file and will import it into this list for display. My intention is that every time a tab character is displayed on the line, the font size of the line is reduced by 4 (starting at font size 24). So all lines without tabs will be 24, all lines with 1 tab will be 20, all lines will be 2 tabs 16, etc.). Ideally I would like to set the style of the line every time I add a line to the list (at least I think this would be the simplest).

So, ideally I would see something like this:

String(Font Size 24)

    String(Font Size 20)

      String(Font Size 16)

    String(Font Size 20)

      String(Font Size 16)

         And so on.....

      

I am very new to WPF and I find it very difficult to manipulate it with XAML at the moment. I can't seem to find a command to make each line individual to style.

+3


source to share


1 answer


First, we need to adjust ItemTemplate

to accommodate our management style

<ListView.ItemTemplate>
   <DataTemplate>
      <TextBlock>
   <DataTemplate>
</ListView.ItemTemplate>

      

Second, what FontSize

does it depend on ? The string itself. This means we need to bind it in the element template:

<TextBlock FontSize="{Binding Text}"/> //Could be "." if binding to List<String>

      

Note that we can do this because it FontSize

is a dependency property. Finally, the text is clearly not a number, so we need a converter to change it to one:



<TextBlock FontSize="{Binding Path=Text,
                              Converter={StaticResource TabCountStringConverter}}"/> 

public class TabCountStringConverter : IValueConverter
{
    public object Convert(...)
    {
         return (value as String).Count(c => c == '\t'); //Count tabs
    }

    public object ConvertBack(...)
    {
         return Binding.DoNothing;
    }
}

      

I don't pretend to have a tab counting feature, but this is a good start :) He needs an element to change the number of tabs to the correct font size, maybe Dictionary

. The implementation is really up to you.

You can also set this on a row container control, as it must also apply to nested controls. See MSDN .

To try and clear up the confusion from the comments:

  • "Path =". means "Snap to the object itself". Since you already have the string in question as a data context, you don't want to bind to its property, you want to bind it to the string.

  • Static resources must be defined in the collection of Resources

    your container; eg:

    <UserControl.Resources>
       <local:TabCountStringConverter x:Key="TabCountStringConverter"/>
    </UserControl.Resources>
    
          

    Where "local" was previously defined as xmlns

    . The return value of the converter is used when evaluating the binding, what we're doing here is telling the framework which converter to use. If we don't do one (as seen above), you will get a not found resource exception.

+2


source







All Articles