Each entry in my listbox has the template below. How do I programmatically set the tooltip for Price and for ProductName? What ids can I use? Please note that I want to distinguish the 2 items: Price and ProductName and display a different tooltip for each even though they belong to the same entry in listbox.
As you see, setting the tooltip in xaml (as seen for Price below) is straightforward. But I need the flexibility of setting it dynamically. Thanks.
<DataTemplate>
<DockPanel >
<TextBlock DockPanel.Dock="Left" Text = "{Binding ProductName}" />
<TextBlock Text=" " />
<TextBlock Text = "{Binding Price}" ToolTip="Price" />
</DockPanel>
</DataTemplate>
You can bind the tooltip (you can bind almost any property) so that when you assign to the property the UI will update. Your best bet would be to make a property somewhere (like the object that has your Price property) and bind to that critter. Just make sure that you use a DependencyProperty
or use INotifyPropertyChanged
.
<DataTemplate>
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text = "{Binding ProductName}" />
<TextBlock Text=" " />
<TextBlock Text = "{Binding Price}" ToolTip="{Binding PriceTooltipProperty}" />
</DockPanel>
</DataTemplate>
You could use a converter, converters take in a type of object, then you could check what that is? ie a string or a number and return a string with whatever tooltip you wanted.
{Binding Path=Price, Converter={StaticResource ObjectToTooltipConverter}}
Then your converter could look something like this.
public class ObjectToTooltipConverter: IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is Decimal)
{
return "The value was a decimal";
}
if(value is String)
{
return "The value was a string";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.