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

C# (WPF)如何在ItemsControl中的项目之间添加分隔符-错误修复,c#,wpf,xaml,C#,Wpf,Xaml,我在这个网站上找到了解决方案(我如何在ItemsControl()和FoundBug=(() 这是: 当我尝试调整ItemsControl(我已将属性“HorizontalScrollBarVisibility”设置为“Disable”)的大小时,它会出现有没有办法解决这个问题?这并不容易,但我意识到我应该怎么做才能解决这个问题。我的想法是使用自定义转换器,但我没有任何聪明的想法如何将ConverterParameter发送到转换器。但我找到了解决方案。 这是我的转换器: public cla

我在这个网站上找到了解决方案(我如何在ItemsControl()和FoundBug=(() 这是:


当我尝试调整ItemsControl(我已将属性“HorizontalScrollBarVisibility”设置为“Disable”)的大小时,它会出现有没有办法解决这个问题?

这并不容易,但我意识到我应该怎么做才能解决这个问题。我的想法是使用自定义转换器,但我没有任何聪明的想法如何将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;
    }
}
公共类分隔转换器:IValueConverter
{
私人能见度(可视);;
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
var元素=(TextBlock)值;
element.Loaded+=ElementLoaded;
返回可见;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回null;//不需要
}
私有静态void元素已加载(对象发送方、路由目标)
{
var元素=发送方作为文本块;
var parentItemsControl=element.FindParent(x=>x是ItemsControl)作为ItemsControl;
var parentPanel=element.FindParent(x=>x是Panel)作为Panel;
if(parentItemsControl==null | | element==null | | parentPanel==null)
回来
var itemText=parentPanel.FindName(“MyTextBlock”)作为TextBlock;
var collection=parentItemsControl.ItemsSource作为IEnumerable;
if(itemText==null | | collection==null)
回来
var list=collection.ToList();
if(list.IndexOf(itemText.Text)==list.Count()-1)//可能不正确,因为我们可以有两个相同的项
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;
    }
公共静态DependencyObject FindParent(此DependencyObject元素,Func筛选器)
{
DependencyObject parent=VisualTreeHelper.GetParent(元素);
如果(父项!=null)
{
if(过滤器(父级))
{
返回父母;
}
返回FindParent(父级、筛选器);
}
返回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有其他想法,你可以在这里发布你的答案=)