如何在 Silverlight 中为每个(动态)列表框项目设置多行工具提示

发布于 2024-12-07 19:11:39 字数 4174 浏览 2 评论 0原文

我想在 Silverlight 中为 ListBox 中的每个项目设置一个包含多行的工具提示。

在 XAML 中,我有一个列表框和一个按钮:

<ListBox Height="100" HorizontalAlignment="Left" Margin="24,136,0,0" Name="listBox1" VerticalAlignment="Top" Width="109" ItemsSource="{Binding}">
<Button Content="Add"  Name="button_add" VerticalAlignment="Top" Width="118" Click="add_Click">

在 c# 中,

private void button_add_Click(object sender, RoutedEventArgs e)
    {
        ObservableCollection<Person> obs1 = new ObservableCollection<Person>();
        obs1.Add(new Person(){Name="Name1", Age=1});
        obs1.Add(new Person(){Name="Name2", Age=2});
        listBox1.ItemsSource = obs1;
        // This Line of Code MUST NOT BE USED!! listBox1.DisplayMemberPath = "Name";          
    }
public class Person
{
    public String Name { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

我想为每个项目显示年龄作为我的工具提示。

编辑:好的,这是仅将 AGE 显示为工具提示的解决方案。那是一行/排。

                <ListBox Height="100" HorizontalAlignment="Left" Margin="24,136,0,0" Name="listBox1" VerticalAlignment="Top" Width="109" ItemsSource="{Binding}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                                <TextBlock Text="{Binding Name}" ToolTipService.ToolTip="{Binding Age}"/>
                            </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

但是如果我想显示 2 行工具提示怎么办?类似于:

 Name: Name1
 Age: 1

编辑:3 行,2 列。我还添加了 public String Comment { set;得到; 。

                    <ListBox Height="100" HorizontalAlignment="Left" Margin="24,136,0,0" Name="listBox1" VerticalAlignment="Top" Width="109" ItemsSource="{Binding}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <ToolTipService.ToolTip>
                                    <ToolTip>
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="20"></RowDefinition>
                                                <RowDefinition Height="20"></RowDefinition>
                                                <RowDefinition Height="40"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="80"/>
                                                <ColumnDefinition Width="250"/>

                                            </Grid.ColumnDefinitions>
                                            <TextBlock Text="Name: " Grid.Row="0" Grid.Column="0" />
                                            <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
                                            <TextBlock Text="Age: " Grid.Row="1" Grid.Column="0"/>
                                            <TextBlock Text="{Binding Age}" Grid.Row="1" Grid.Column="1"/>
                                            <TextBlock Text="Comment: " Grid.Row="2" Grid.Column="0"/>
                                            <TextBlock Text="{Binding Comment}" Grid.Row="2" Grid.Column="1" TextWrapping="Wrap" />
                                        </Grid>
                                    </ToolTip>
                                </ToolTipService.ToolTip>
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

但是仍然有一个问题 注释可以短也可以长,所以我想使注释的行/行/空间可变,否则文本将被切断。

I would like to set in Silverlight for each item in a ListBox a tooltip with many rows.

In XAML i have a listbox and a button:

<ListBox Height="100" HorizontalAlignment="Left" Margin="24,136,0,0" Name="listBox1" VerticalAlignment="Top" Width="109" ItemsSource="{Binding}">
<Button Content="Add"  Name="button_add" VerticalAlignment="Top" Width="118" Click="add_Click">

in c#

private void button_add_Click(object sender, RoutedEventArgs e)
    {
        ObservableCollection<Person> obs1 = new ObservableCollection<Person>();
        obs1.Add(new Person(){Name="Name1", Age=1});
        obs1.Add(new Person(){Name="Name2", Age=2});
        listBox1.ItemsSource = obs1;
        // This Line of Code MUST NOT BE USED!! listBox1.DisplayMemberPath = "Name";          
    }
public class Person
{
    public String Name { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

I would like to show for each item the Age as my tooltip.

Edit: Ok, this is the Solution for showing only AGE as tooltip. That is one Line/Row.

                <ListBox Height="100" HorizontalAlignment="Left" Margin="24,136,0,0" Name="listBox1" VerticalAlignment="Top" Width="109" ItemsSource="{Binding}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                                <TextBlock Text="{Binding Name}" ToolTipService.ToolTip="{Binding Age}"/>
                            </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

But what if i want to show a Tooltip with 2 Lines? something like:

 Name: Name1
 Age: 1

Edit: 3 rows, 2 columns. i also added public String Comment { set; get; } to class Person

                    <ListBox Height="100" HorizontalAlignment="Left" Margin="24,136,0,0" Name="listBox1" VerticalAlignment="Top" Width="109" ItemsSource="{Binding}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <ToolTipService.ToolTip>
                                    <ToolTip>
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="20"></RowDefinition>
                                                <RowDefinition Height="20"></RowDefinition>
                                                <RowDefinition Height="40"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="80"/>
                                                <ColumnDefinition Width="250"/>

                                            </Grid.ColumnDefinitions>
                                            <TextBlock Text="Name: " Grid.Row="0" Grid.Column="0" />
                                            <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
                                            <TextBlock Text="Age: " Grid.Row="1" Grid.Column="0"/>
                                            <TextBlock Text="{Binding Age}" Grid.Row="1" Grid.Column="1"/>
                                            <TextBlock Text="Comment: " Grid.Row="2" Grid.Column="0"/>
                                            <TextBlock Text="{Binding Comment}" Grid.Row="2" Grid.Column="1" TextWrapping="Wrap" />
                                        </Grid>
                                    </ToolTip>
                                </ToolTipService.ToolTip>
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

But there is stil a problem. The comment can be short or long, so i would like to make the ROWS/LINES/SPACE for Comment to be variable or else Text is cut off.

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

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

发布评论

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

评论(1

无声无音无过去 2024-12-14 19:11:39

您可以对工具提示内容使用任何控件:

<ToolTipService.ToolTip>
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding Name}" />
        <TextBlock Text="{Binding Age}" />
    </StackPanel>
</ToolTipService.ToolTip>

另一种可能性是使用转换器,它返回由 \r\nName 和 Age >。

You can use any controls for tooltip content:

<ToolTipService.ToolTip>
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding Name}" />
        <TextBlock Text="{Binding Age}" />
    </StackPanel>
</ToolTipService.ToolTip>

Other possibility would be to use a converter, that returns the Name and Age separated by \r\n.

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