开发者

Styling Items based on group sizes

开发者 https://www.devze.com 2023-02-09 21:56 出处:网络
I am fairly new to WPF so forgive me if I am missing something obvious. I\'m having a pr开发者_如何学Coblem where I have a collection of AggregatedLabels and I am trying to bind the ItemCount of each

I am fairly new to WPF so forgive me if I am missing something obvious. I'm having a pr开发者_如何学Coblem 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" />


With your CollectionView in place you might be able to bind to the Groups property, i've never used that, will try it and clarify if possible...

Edit: Allright, here's one way to do it:

The data you bind to needs to be the CollectionView.Groups, the CollectionView should be defined like this:

CollectionView view = (ListCollectionView) CollectionViewSource.
    GetDefaultView(LabelData);
view.GroupDescriptions.Add(new PropertyGroupDescription("Name"));

Then you can bind to the respective properties of CollectionViewGroup in code, what you need are probably:

  1. ItemCount
  2. Name

That being said your original binding should work.

Note: You only pass one value to the converter, the ItemCount, thus it should look like this:

public class CountToFontSizeConverter : IValueConverter
{
    #region IValueConverter Members

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

        if ((minFontSize + (int)value + increment) < maxFontSize)
        {
            return (double)(minFontSize + (int)value + increment);
        }
        return (double)maxFontSize;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Edit: Further clarifications...

Just add the CollectionView to your ViewModel as a property and create it in its constructor:

public class TagCloudViewModel//:INotifyPropertyChanged
{
    public ObservableCollection<AggregatedLabelModel> AggregatedLabels 
        {get; set;}
    public CollectionView AggregatedLabelsView {get; set;} // <-This...

    public TagCloudViewModel()
    {
        var data = new DataAccess();
        AggregatedLabels = data.GetData();

        //...and this:
        AggregatedLabelsView  = (ListCollectionView)CollectionViewSource.
            GetDefaultView(AggregatedLabels);
        AggregatedLabelsView.GroupDescriptions.Add(
            new PropertyGroupDescription("Name"));
    }
}

Then bind to AggregatedLabelsView.Groups.

0

精彩评论

暂无评论...
验证码 换一张
取 消