WPF DataGrid单元工具提示绑定到自定义数据

时间:2014-11-07 11:21:40

标签: c# wpf xaml datagrid tooltip

抱歉错误,英语不是我的母语。

我正在尝试为DataGrid单元格制作自定义工具提示。我正在尝试这个:

<Window.Resources>
    <Style TargetType="{x:Type DataGridCell}">           
        <Setter Property="ToolTip">
            <Setter.Value>
                <ToolTip Content="{Binding Hint}"/>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
    <DataGrid x:Name="dgMain" AutoGenerateColumns="True" Grid.Row="1" RowHeaderWidth ="0" BorderBrush="Aqua" BorderThickness="1" 
              HorizontalGridLinesBrush="Silver" VerticalGridLinesBrush="Silver" RowBackground="LightGray" AlternatingRowBackground="White">

        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}" >
                <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
                <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>

这是最好的结果

public class MyDataTableItem
{
        public string MainText { get; set; }
        public string Hint { get; set; }
        public override string ToString()
        {
            return MainText;
        }
        public MyDataTableItem(string MainText, string Hint)
        {
            this.MainText = MainText;
            this.Hint = Hint;
        }
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    // Actually, there is can be unknown number of items
    string[] DocumentList = { "Doc 1", "Doc 2", "Doc 3", "Doc 4" };
    MyDataTableItem[,] Revisions = new MyDataTableItem[1, 4];

    Revisions[0, 0] = new MyDataTableItem("A01", "123");
    Revisions[0, 1] = new MyDataTableItem("A02", "234534");
    Revisions[0, 2] = new MyDataTableItem("A03", "afdf");
    Revisions[0, 3] = new MyDataTableItem("A04", "ferhbr6");

    dt = new DataTable();

    foreach (var name in DocumentList)
    {
        var column = new DataColumn();
        column.DataType = typeof(MyDataTableItem);
        column.ColumnName = name;
        column.ReadOnly = true;
        column.Unique = false;
        dt.Columns.Add(column);
    }
    var row = dt.NewRow();
    int i = 0;
    foreach (var name in DocumentList)
    {
        row[name] = Revisions[0, i];
        i++;
    }
    dt.Rows.Add(row);

    dgMain.ItemsSource = dt.DefaultView;
}

这不起作用,工具提示按预期为空。如何将工具提示绑定到MyDataTableItem的Hint属性?

0 个答案:

没有答案