工具提示用于显示集合中项目的索引.C# WPF MVVM [英] ToolTip use to show the index of an item from a collection. C# WPF MVVM

查看:19
本文介绍了工具提示用于显示集合中项目的索引.C# WPF MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 256 个值的数组绘制灰度图像的直方图.我这样做是通过创建我自己的带有 256 个垂直矩形(列)的图表来实现的.我的目标是,当我将鼠标悬停在一个矩形上时,我会从它所在的数组中获取它的索引值,就像我将鼠标悬停在第 200 个矩形上一样,我在光标附近的一个小文本框中得到 200,就像 ToolTip 应该如何执行一样.问题是我没有找到 ToolTip 的正确绑定来使其正常工作.我认为解决方案在于正确使用 AlternationCount/AlternationIndex

I'm plotting a histogram of a grayscale image using an array of 256 values. Im doing so by creating my own chart with 256 vertical rectangles (columns). My aim is that when I mouse over one rectangle I get its index value, its index from the array it lies in, like if I mouse over the 200th rectangle I get 200 in a small text box near cursor, like how ToolTip should perform. The problem is that I don't find the proper binding for ToolTip to get this working. I think the solution lies in the proper use of AlternationCount / AlternationIndex

这里可能是 XAML 代码,也许有人可以给我一个修复:

Here is may XAML code, maybe someone can give me a fix for it:

<ItemsControl Grid.Row="1" ItemsSource="{Binding HistogramValues}" AlternationCount="{Binding Path=HistogramValues.Count}">              
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Height="{Binding }" ToolTip="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=ItemsControl}}" Width="2" VerticalAlignment="Bottom" Fill="Black"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate> 
</ItemsControl>

我在视图模型中的位置:

where in view model I have:

public float[] HistogramValues { get; set; }

我发现了这篇有用的帖子编号列表框但我仍然无法为我的案例运行它,我对 ItemTemplateTemplatedParent 感到困惑,我不知道我是否需要或者我是否需要模板我应该如何编码这个.

I've found this useful post Numbered listbox but I still cant make it run for my case, I'm confused about that ItemTemplate and TemplatedParent which I dont know if I need or if I need the template how should I code this.

推荐答案

如果你对转换器不满意,你可以使用这个,

If you are not ok with converters you can use this,

 <ItemsControl Grid.Row="1" ItemsSource="{Binding Collection}" AlternationCount="{Binding Path=Collection.Count}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock  Text="{Binding}" ToolTip="{Binding Path=(ItemsControl.AlternationIndex),   RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContentPresenter}}">
                    </TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

由于 AlternationIndex 是附加属性,所以应该用()"覆盖.

Since AlternationIndex is attached property, it should be covered with "()".

这篇关于工具提示用于显示集合中项目的索引.C# WPF MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆