Binding ListBox ItemCount to IvalueConverter

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2011-02-08T21:00:08Z Indexed on 2011/02/08 23:25 UTC
Read the original article Hit count: 340

Filed under:
|
|

Hi All,

I am fairly new to WPF so forgive me if I am missing something obvious. I'm having a problem where I have a collection of AggregatedLabels and I am trying to bind the ItemCount of each AggregatedLabel to the FontSize in my DataTemplate so that if the ItemCount of an AggregatedLabel is large then a larger fontSize will be displayed in my listBox etc. The part that I am struggling with is the binding to the ValueConverter. Can anyone assist? Many thanks!

XAML Snippet

<DataTemplate x:Key="TagsTemplate">
        <WrapPanel>
            <TextBlock Text="{Binding Name, Mode=Default}" 
                       TextWrapping="Wrap" 
                       FontSize="{Binding ItemCount, Converter={StaticResource CountToFontSizeConverter}, Mode=Default}" 
                       Foreground="#FF0D0AF7"/>
        </WrapPanel>
    </DataTemplate>

<ListBox x:Name="tagsList" 
             ItemsSource="{Binding AggregatedLabels, Mode=Default}" 
             ItemTemplate="{StaticResource TagsTemplate}" 
             Style="{StaticResource tagsStyle}" 
             Margin="200,10,16.171,11.88" />

AggregatedLabel Collection

using (DB2DataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                AggregatedLabelModel aggLabel = new AggregatedLabelModel();

                aggLabel.ID = Convert.ToInt32(dr["LABEL_ID"]);
                aggLabel.Name = dr["LABEL_NAME"].ToString();

                LabelData.Add(aggLabel);

            }
        }

Converter

public class CountToFontSizeConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        const int minFontSize = 6;
        const int maxFontSize = 38;
        const int increment = 3;
        int count = (int)value;

        if ((minFontSize + count + increment) < maxFontSize)
        {
            return minFontSize + count + increment;            
        }
        return maxFontSize;

    }

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

AggregatedLabel Class

public class AggregatedLabelModel
{
    public int ID { get; set; }
    public string Name { get; set; }

}

CollectionView

ListCollectionView labelsView = new ListCollectionView(LabelData);

labelsView.GroupDescriptions.Add(new PropertyGroupDescription("Name"));

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf