WPF工具提示数据绑定

时间:2013-08-02 19:46:32

标签: c# wpf

我正在尝试为ListView行创建工具提示。 用于生成列表视图的当前数据结构:

public class Map
{
    public string Name{get;set;}
    public List<Difficulty> Difficulties{get;set;}
}

public class Difficulty
{
    public int ID{get;set;}
    public string Name{get;set;}
}

// Items to list are added like.
ListView.Items.Add(new Map(){....});

这就是我使用的工具提示XAML代码:

<Setter Property="ToolTip">
    <Setter.Value>
        <ItemsControl ItemsSource="{Binding Difficulties}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding Name}" />
                        <TextBlock Grid.Column="1" Text="{Binding ID}" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Setter.Value>
</Setter>

一切正常,但我需要的是从地图显示名称,而不是难度

1 个答案:

答案 0 :(得分:1)

尝试

Text="{Binding DataContext.Name, RelativeSource={RelativeSource AncestorType=ListViewItem}}"