У меня есть пользовательский элемент управления, который берет некоторые строки и отображает их в пользовательском интерфейсе в зависимости от того, сколько раз строка появляется в моей коллекции. Следовательно, чем больше строк содержится в моей коллекции, тем больше она будет отображаться в пользовательском интерфейсе и наоборот. В любом случае, моя проблема в том, что в данный момент scrollViewer отображает теги, но я бы хотел, чтобы все теги отображались в одном окне без необходимости прокрутки, а также масштабирования, чтобы соответствовать всему окну. Кто-нибудь может помочь с этим? спасибо!
XAML:
<UserControl x:Class="TagCloudDemo.TagCloudControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:TagCloudDemo="clr-namespace:TagCloudDemo">
<UserControl.Resources>
<TagCloudDemo:WeightToSizeConverter x:Key="WeightToSizeConverter" />
</UserControl.Resources>
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<ItemsControl
ItemsSource="{Binding Path=Tags, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TagCloudDemo:TagCloudControl}}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontSize="{Binding Path=Weight, Converter={StaticResource WeightToSizeConverter}}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</UserControl>
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace TagCloudDemo
{
public partial class TagCloudControl : UserControl
{
public TagCloudControl()
{
InitializeComponent();
}
public IEnumerable<string> Words
{
get { return (IEnumerable<string>)GetValue(WordsProperty); }
set { SetValue(WordsProperty, value); }
}
public static readonly DependencyProperty WordsProperty =
DependencyProperty.Register("Words",
typeof(IEnumerable<string>),
typeof(TagCloudControl),
new UIPropertyMetadata(new List<string>(), WordsChanged));
public IEnumerable<Tag> Tags
{
get { return (IEnumerable<Tag>)GetValue(TagsProperty); }
set { SetValue(TagsProperty, value); }
}
public static readonly DependencyProperty TagsProperty =
DependencyProperty.Register("Tags",
typeof(IEnumerable<Tag>),
typeof(TagCloudControl),
new UIPropertyMetadata(new List<Tag>(), TagsChanged));
private static void WordsChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TagCloudControl tagCloudControl = sender as TagCloudControl;
tagCloudControl.Tags = TagCloudDemo.Tag.CreateTags(tagCloudControl.Words);
}
private static void TagsChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TagCloudControl tagCloudControl = sender as TagCloudControl;
WeightToSizeConverter converter = tagCloudControl.FindResource("WeightToSizeConverter") as WeightToSizeConverter;
if (converter != null && tagCloudControl.Tags != null)
{
converter.MaxWeight = tagCloudControl.Tags.Max(t => t.Weight);
}
}
}
public class WeightToSizeConverter : IValueConverter
{
public int MaxWeight { get; set; }
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int weight = (int)value;
return 32 * MaxWeight / weight;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion IValueConverter Members
}
}