I have datagrid control in my project and i want to add tooltip to datagridcells. I customized the tooltip control and put a textbox control in it. I tried to bind cell content value to textbox but it did not work.
When I use the Tooltip contol directly (without customizing) it is worked.
This is xaml code:
Without customizing the tooltip binding is working.
<Window.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=Content.Text}"/>
</Style>
</Window.Resources>
<DataGrid ItemsSource="{Binding Persons}"
AutoGenerateColumns="True"/>
But customized tooltip binding is not working.
<Window.Resources>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip">
<Setter.Value>
<TextBox Text="{Binding RelativeSource={RelativeSource Self},Path=Content.Text}"/>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<DataGrid ItemsSource="{Binding Persons}"
AutoGenerateColumns="True"/>
This is model class:
public class Person
{
public string PersonName { get; set; }
public string Surname { get; set; }
public string Details { get; set; }
}
This is ViewModel class:
public class MainViewVM:INotifyPropertyChanged
{
public ObservableCollection<Person> Persons { get; set; }
public MainViewVM()
{
Persons = new ObservableCollection<Person>
{
new Person()
{
PersonName = "Andrei",
Surname = "Surname 1"
},
new Person()
{
PersonName = "Jack",
Surname = "Surname 2"
},
new Person()
{
PersonName = "Melisa",
Surname = "Surname 3"
}
};
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyOfPropertyChange(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
You need to do something like this:
<Style TargetType="DataGridCell">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBox Text="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ToolTip}}" />
</ToolTip>
</Setter.Value>
</Setter>
</Style>
I have to include ToolTip
to access its PlacementTarget
.
DataContext
return us Person
instance (you can override ToString
or use data template to see its properties in tooltip). Use Content.Text
instead if you want only text from this cell.