P
P
protov2013-12-05 22:53:20
WPF
protov, 2013-12-05 22:53:20

WPF ComboBox length values?

Good afternoon! I can't find a solution to the problem. The question is: I'm loading values ​​for a combobox and some values ​​can be very long and start to look ugly as a long selection menu. I can’t figure out how to limit the number of displayed characters (or the width of the displayed area), so that what doesn’t fit is cut off, but when selected in the combobox value, it is displayed completely. I am attaching a picture to make it clearer what the problem is.
fcf521a6f808.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Kiselev, 2013-12-06
@protov

Make a converter that will cut the strings to the length you need. And use it when deriving values ​​from the model.

<ComboBox ItemsSource="{Binding}">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Converter={StaticResource TrimValueConverter}}"/>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

class TrimValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
            string s = value.ToString();
            
            return s.Substring(0, 100);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question