ListView 元素的代码隐藏工具提示
Code-behind tooltips for ListView elements
我有一个 ListView
填充在代码隐藏中,如下所示:
<ListView x:Name="FruitListView">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock FontSize="20" Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Width" Value="146" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
由于我列表中的水果数量会随时间变化,因此必须在代码隐藏中动态创建工具提示。
public FruitPage()
{
InitializeComponent();
var Fruits = new List<string>();
Fruits.Add("apple");
Fruits.Add("orange");
// Populate the ListView.
FruitListView.ItemsSource = Fruits;
}
我现在想根据文本在每个 ListViewItem 上创建工具提示(例如,代表“apple”、“orange”等的 ListViewItem 的不同工具提示)。
由于 ListView
的 ItemSource
是动态填充的,我认为这些工具提示也必须在代码隐藏中完成,而不是在 XAML 中完成。
问题是 - 如何抓取 ListViewItem
以向其添加工具提示?我试过以下代码:
foreach (var item in FruitListView.Items)
{
// item.ToolTip = new ToolTip() { Content = "Test" };
}
...但是“项目”是 string
,而不是 ListViewItem
。
如何“抓取”动态填充的 ListView
中的每个 ListViewItem
以向它们添加(不同的)工具提示?或者,我可以使用什么其他控件来实现此目的?
从 ItemContainerStyle
设置 ToolTip
并将其绑定到项目,然后使用 IValueConverter
(Data conversion) 提供基于您的工具提示值限制条件:
<ListBox>
<ListBox.Resources>
<ItemToToolTipConverter x:Key="ItemToToolTipConverter" />
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ToolTip"
Value="{Binding Path=., Converter={StaticResource ItemToToolTipConverter}}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
class ItemToToolTipConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
value is string stringValue
? stringValue.StartsWith("a", StringComparison.OrdinalIgnoreCase)
? "Fruity begins with 'a'"
: "Some random fruit"
: Binding.DoNothing;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
我有一个 ListView
填充在代码隐藏中,如下所示:
<ListView x:Name="FruitListView">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock FontSize="20" Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Width" Value="146" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
由于我列表中的水果数量会随时间变化,因此必须在代码隐藏中动态创建工具提示。
public FruitPage()
{
InitializeComponent();
var Fruits = new List<string>();
Fruits.Add("apple");
Fruits.Add("orange");
// Populate the ListView.
FruitListView.ItemsSource = Fruits;
}
我现在想根据文本在每个 ListViewItem 上创建工具提示(例如,代表“apple”、“orange”等的 ListViewItem 的不同工具提示)。
由于 ListView
的 ItemSource
是动态填充的,我认为这些工具提示也必须在代码隐藏中完成,而不是在 XAML 中完成。
问题是 - 如何抓取 ListViewItem
以向其添加工具提示?我试过以下代码:
foreach (var item in FruitListView.Items)
{
// item.ToolTip = new ToolTip() { Content = "Test" };
}
...但是“项目”是 string
,而不是 ListViewItem
。
如何“抓取”动态填充的 ListView
中的每个 ListViewItem
以向它们添加(不同的)工具提示?或者,我可以使用什么其他控件来实现此目的?
从 ItemContainerStyle
设置 ToolTip
并将其绑定到项目,然后使用 IValueConverter
(Data conversion) 提供基于您的工具提示值限制条件:
<ListBox>
<ListBox.Resources>
<ItemToToolTipConverter x:Key="ItemToToolTipConverter" />
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ToolTip"
Value="{Binding Path=., Converter={StaticResource ItemToToolTipConverter}}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
class ItemToToolTipConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
value is string stringValue
? stringValue.StartsWith("a", StringComparison.OrdinalIgnoreCase)
? "Fruity begins with 'a'"
: "Some random fruit"
: Binding.DoNothing;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotSupportedException();
}