(WPF)如何在ItemsControl中的项之间添加分隔符 - 错误修复

时间:2011-02-25 12:54:23

标签: c# wpf xaml

我在这个网站上找到了解决方案(如何在ItemsControl(click)中的项之间输入分隔符并找到bug =((( 这是它:

enter image description here

当我尝试调整ItemsControl的大小时(我已将属性“Horizo​​ntalScrollBarVisibility”设置为“禁用”),这很有意义。任何想法我应该如何修复此错误?

1 个答案:

答案 0 :(得分:0)

这并不容易,但我意识到我该怎么做才能解决这个问题。我的想法是使用自定义转换器,但我没有任何聪明的想法如何将ConverterParameter发送到转换器。但我找到了解决方案。 这是我的转换器:

public class SeparatorConverter : IValueConverter
{
    private Visibility _visible;
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var element = (TextBlock) value;
        element.Loaded += ElementLoaded;
        return _visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null; //not needed
    }
    private static void ElementLoaded(object sender, RoutedEventArgs e)
    {
        var element = sender as TextBlock;
        var parentItemsControl = element.FindParent(x => x is ItemsControl) as ItemsControl;
        var parentPanel = element.FindParent(x => x is Panel) as Panel;

        if (parentItemsControl == null || element == null || parentPanel== null)
            return;

        var itemText = parentPanel.FindName("MyTextBlock") as TextBlock;
        var collection = parentItemsControl.ItemsSource as IEnumerable<string>;

        if (itemText == null || collection == null)
            return;

        var list = collection.ToList();
        if (list.IndexOf(itemText.Text) == list.Count() - 1) // Can be incorrect because we can have two same items
           element.Visibility = Visibility.Collapsed;
    }
}

查找父函数:

public static DependencyObject FindParent(this DependencyObject element, Func<DependencyObject, bool> filter)
    {
        DependencyObject parent = VisualTreeHelper.GetParent(element);

        if (parent != null)
        {
            if (filter(parent))
            {
                return parent;
            }

            return FindParent(parent, filter);
        }

        return null;
    }

这是我的XAML代码:

  <Coverters:SeparatorConverter x:Key="SeparatorVisibilityConverter">
    </Coverters:SeparatorConverter>
    <ItemsControl Grid.Row="1" ItemsSource="{Binding Value, IsAsync=False}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel>
            </WrapPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBlock x:Name="MyTextBlock" Text="{Binding}" Style="{StaticResource TextBlockInListViewItem}"  TextWrapping="WrapWithOverflow"/>
                <TextBlock x:Name="commaTextBlock" Style="{StaticResource TextBlockInListViewItem}" Text=", " Visibility="{Binding RelativeSource={RelativeSource Self},
                            Converter={StaticResource SeparatorVisibilityConverter}}"/>
            </WrapPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

确定。无论如何,如果你有另一个想法如何修复这个bug,你可以在这里发布你的答案=)