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

发布于 2024-10-19 03:11:47 字数 364 浏览 0 评论 0原文

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

在此处输入图像描述

当我尝试调整 ItemsControl 的大小时会发生这种情况(我已将属性“Horizo​​ntalScrollBarVisibility”设置为“禁用”)任何想法我应该如何修复这个错误?

I found solution on this site(How can I input separator between items in an ItemsControl(click) And found bug=(((
Here is it:

enter image description here

It hapens when I trying to resize ItemsControl(I've set property "HorizontalScrollBarVisibility" to "Disable") Any ideas How should I fix this bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2024-10-26 03:11:47

这并不容易,但我意识到我应该做什么来修复这个错误。我的想法是使用自定义转换器,但我没有任何聪明的想法如何将 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>

好的。无论如何,如果您有其他想法如何修复此错误,您可以在这里发布您的答案=)

It was not easy,but I realized what shall I do to fix this bug. My idea is to use custom Convertor, but I haven't any clever thoughts How to send ConverterParameter into convertor. But I've found solution.
Here is my convertor:

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;
    }
}

Find parent function:

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;
    }

Here is my XAML code:

  <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>

Ok. Any way, If you have another idea how to fix this bug, you could post your answer here=)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文