How do I hide the empty tooltip in Silverlight? - c#

I have a TextBlock with a tooltip that displays the same data, in case of truncation. However if the property that TextBlock.Text and the tooltip's text are bound to is empty (null or zero length string) the tooltip appears as a small empty box. Is there a way to hide this and show no tooltip in this case?
<TextBlock Text="{Binding Text}">
<util:ToolTipManager.ToolTip>
<TextBlock TextWrapping="Wrap" Text="{Binding Text}" />
</util:ToolTipManager.ToolTip>
</TextBlock>
I have tried using a StringToVisibilityConverter by adding Visibility="{Binding Text, Converter={StaticResource StringToVisConverter}}" to the TextBlock without any luck.
I also tried implementing the answer given Hide tooltip if binding is null but that seems specific to their set-up (or at least I haven't figured out how to adapt it successfully).
(ToolTipManager is from http://www.codeproject.com/Articles/36078/Silverlight-2-0-How-to-use-a-DataBinding-with-the, used to provide the data binding for the tooltip.)
Edit:
In response to the comments, here is the XAML I tried for the related question mentioned above:
<TextBlock Text="{Binding PointName}">
<local:ToolTipManager.ToolTip>
<Grid>
<TextBlock TextWrapping="Wrap" Text="{Binding PointName}"/>
<Rectangle Fill="Transparent" Visibility="{Binding PointName, Converter={StaticResource StringToVisConverter}}" />
</Grid>
</local:ToolTipManager.ToolTip>
</TextBlock>
And here is my String to Visibility converter code:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string visible = (string)value;
return (!String.IsNullOrWhiteSpace(visible) ? Visibility.Visible : Visibility.Collapsed);
}

I assume you are using Siverlight 4 as it is one of your tags.
In Silverlight 4, I am pretty sure that you don't need the ToolTipManager anymore.
You can just wrap the Rectangle and the TextBlock with a Grid, like this,
<Grid>
<TextBlock Text="{Binding PointName}"/>
<Rectangle Fill="Transparent" Visibility="{Binding PointName, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTipService.ToolTip="{Binding PointName}"/>
</Grid>
UPDATE:
<Grid>
<TextBlock Text="{Binding PointName}"/>
<Rectangle Fill="Transparent" Visibility="{Binding PointName, Converter={StaticResource BooleanToVisibilityConverter}}">
<ToolTipService.ToolTip>
<TextBlock TextWrapping="Wrap" Text="{Binding PointName}"/>
</ToolTipService.ToolTip>
</Rectangle>
</Grid>

If you are using Silverlight 5, the following seems to work:
<ToolTipService.ToolTip>
<ToolTip Visibility="{Binding WhatDeterminesTooltipVisibility}">
<Border Background="Azure" Width="100" />
</ToolTip>
</ToolTipService.ToolTip>

Related

Long text in tooltip is not fully shown in UWP

I've defined a for a listbox item, and binded the text inside the tooltip to two properties of the object (name + description) but i have an issue that the text is being cut off
here is my tooltip:
<ToolTipService.ToolTip>
<StackPanel >
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13">
<Bold>Name</Bold>
</TextBlock>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13" TextWrapping="Wrap">
<Bold>Description</Bold>
</TextBlock>
<TextBlock Text="{x:Bind description}"/>
</StackPanel>
</StackPanel>
</ToolTipService.ToolTip>
Now the thing is, if i bind the tooltip to a method that returns the name + description (Which is how it was previously, but was super ugly) it does show all the text, it was like this:
<ToolTipService.ToolTip>
<TextBlock Text="{x:Bind Description}"/>
</ToolTipService.ToolTip>
But I needed to style it to make it look better, so i've tried to do what was posted above.
I've already tried setting the Width/Height to super large values, didn't do anything.
any ideas?
The tooltip template probably has a default maximum width, which cuts off the TextBlock. To solve this, just add TextWrapping attribute:
<TextBlock TextWrapping="Wrap" Text="{x:Bind description}"/>
Now the tooltip text will wrap on multiple lines as necessary

How to bind Background to the StackPanel

I am developing Windows8 store app.I have Grid which is populating dynamically
<Grid Grid.Column="1" Margin="0,16,0,0" HorizontalAlignment="Left" VerticalAlignment="Center">
<GridView x:Name="chapterlist" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" ItemClick="onChapterClick" Padding="0" Height="600" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Width="260" Height="80" Background="{Binding RelativeSource={RelativeSource Self}, Path=alreadyDownload, Converter={StaticResource ColorConverter}}">
<TextBlock x:Name ="AAA" Text="{Binding Path=Chapter}" FontSize="10" Foreground="White" d:LayoutOverrides="Width" Margin="5" TextWrapping="Wrap" />
<TextBlock Text="{Binding Path=Name}" Foreground="White" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
<TextBlock Text="{Binding Path=alreadyDownload}" Foreground="#073363" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,18,2,2" FontSize="10" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
So i have to change the background color of StackPanel according to TextBlock value like
<TextBlock Text="{Binding Path=alreadyDownload}" Foreground="#073363" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,18,2,2" FontSize="10" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
I have used ColorConverter like
class ColorConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, String culture)
{
if (value != null)
{
if (value.Equals("Already Downloaded "))
return Colors.Red;
else
return Colors.White;
}
return Colors.White;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
But my grid is not reflecting background color(infact no color at all,its transparent).Why it is happening? How can I solve this problem? Please help.I am attaching image for reference. Thanks in advance
So what I want is to show Grid with text Already Downloaded should be having some other color and rest of the Grids with different color.
There are two ways to do the above mentioned scenerio
1)We can add Background property to the objects that populates ObservableCollection and using binding in xaml
<Grid Width="200" Background="{Binding Background}" />
This way we can choose every item color in the grid and change it dynamically just changing object property. Here Background property must be a string assigned with a valid color like
object.Background = "White"
2)Using Converter(as I used) to convert some existing property in your object to a color(refer my Converter class). We can also bind using some property like I used here
<TextBlock Text="{Binding Path=alreadyDownload}"
which'll look like
<StackPanel Width="260" Height="80" Background="{Binding RelativeSource={RelativeSource Self}, Path=alreadyDownload, Converter={StaticResource ColorConverter}}">
<TextBlock x:Name ="AAA" Text="{Binding Path=Chapter}" FontSize="10" Foreground="White" d:LayoutOverrides="Width" Margin="5" TextWrapping="Wrap" />
<TextBlock Text="{Binding Path=Name}" Foreground="White" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
<TextBlock Text="{Binding Path=alreadyDownload}" Foreground="#073363" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,18,2,2" FontSize="10" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
</StackPanel>
So why my code is not running? Its because the IValueConverter returns object which should be a string, so instead of using
if (value.Equals("Already Downloaded "))
return Colors.Red;
else
return Colors.White;
use
if (value.Equals("Already Downloaded "))
return "#FF0000";
else
return "#FFFFFF";
So its running perfectly
if (value.Equals("Already Downloaded "))
Is it maybe the space between Downloaded and " ?
Use this code instead:
if (value.Equals("Already Downloaded "))
return Brushes.Red;
else
return Brushes.White;
Or If you want to use colors class you can use in this way.
new SolidColorBrush(Colors.Red)
Hope this may be solve your issue.

set visibility of image dynamically in expanderview

I am trying to implement something where I need to display list of people and a green icon if they are online. these people are grouped by some categories. I am using an expanderview toolkit control to display the list. So how do I set the icon image to be visible dynamically ? I have tried something like this which didnt work.
<DataTemplate x:Key="groupsItemTemplate">
<StackPanel Orientation="Horizontal" Margin="30,5,0,0"">
<Image Height="30" Width="30" Source="/Assets/Online.png" Margin="10,5,0,0" Visibility="{Binding IsFriendOnline}"></Image>
<TextBlock TextWrapping="NoWrap" FontFamily="Segoe WP Light" FontSize="24" Margin="8,0,0,0" VerticalAlignment="Center" HorizontalAlignment="left" Height="auto" Width="300" Text="{Binding FriendName}"></TextBlock>
</StackPanel>
</DataTemplate>
IsFriendOnline is an integer property.
Firstly, you need to use a converter in order to convert the value of your IsFriendOnline property to the Visibility enum that you require.
WPF has a "BooleanToVisibilityConverter" built in so if you have the ability to change the IsFriendOnline to a boolean value (which sounds like it makes a little more sense anyway) I would go down this route... if its imperative that the property is an integer then you will need to roll your own converter which isnt difficult.
The syntax would look something like this when you have a converter (my code below assumes IsFriendOnline is a boolean)...
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<DataTemplate x:Key="groupsItemTemplate">
<StackPanel Orientation="Horizontal" Margin="30,5,0,0"">
<Image Height="30" Width="30" Source="/Assets/Online.png" Margin="10,5,0,0" Visibility="{Binding IsFriendOnline, Converter={StaticResource BooleanToVisibilityConverter}}"></Image>
<TextBlock TextWrapping="NoWrap" FontFamily="Segoe WP Light" FontSize="24" Margin="8,0,0,0" VerticalAlignment="Center" HorizontalAlignment="left" Height="auto" Width="300" Text="{Binding FriendName}"></TextBlock>
</StackPanel>
</DataTemplate>
Hope this helps...

WPF DataTemplate method binding with parameter

I have a custom ItemTemplate for a ListBox and I need to "bind" a TextBlock to some special method / property.
My ListBox Source is an ObservableCollection<SearchResultItem>. SearchResultItem containing some properties.
The text need to change based on the value of another object. E.G if this object equals "foo" I need the text value to call the method GetProperty("foo") on the SearchResultItem to get the correct value.
Here is a sample of code:
<DataTemplate>
..
//Here is a Label bound to the Date Property of the SearchResultItem
<Label Margin="2,2,2,0" Grid.Row="0" Grid.Column="2" Content="{Binding Path=Date}" HorizontalAlignment="Right" HorizontalContentAlignment="Right" />
//Here is the textblock that needs to call the method with the parameter based on the value of the other object.
<TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis" Grid.Row="0" Grid.Column="1" Text="I need some help there" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
..
</DataTemplate>
Do you have any idea on how to do that or a better way to do it?
Edit:
-Let's assume SearchResultItem comes from an external library and only exposes the GetProperty method.
-Let's say the "foo" value comes from ConfigurationManager.AppSettings["propertyName"]; if it helps.
OK, because your properties never change, here's one solution. You can do this via another property on your SearchResultItem class:
public string MyTextBinding
{
get
{
return myDictionary.ContainsKey("foo") ? return myDictionary["foo"] : return "myDictionary doesn't contain foo";
}
}
Then just bind your textbox to this property:
<DataTemplate>
<Label Margin="2,2,2,0" Grid.Row="0" Grid.Column="2" Content="{Binding Path=Date}" HorizontalAlignment="Right" HorizontalContentAlignment="Right" />
<TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis" Grid.Row="0" Grid.Column="1" Text="{Binding Path=MyTextBinding, Mode=OneWay}" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
</DataTemplate>
Just use a IValueConverter that takes a SearchResultItem and return the expected text
<TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis"
Grid.Row="0" Grid.Column="1"
Text="{Binding Path=.,
Converter={StaticResource PropertyValueFromSearchResultItemConverter},
ConverterParameter=Foo}"
HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>

How to change datatemplate through code

I have ListBox and DataTemplate
I need Set GroupBox Heigth = 300
How to do it?
<DataTemplate x:Key="data_template">
<GroupBox Header="Категория" Width="300" HorizontalAlignment="Stretch" x:Name="GroupBox">
<DockPanel Tag="{Binding id}">
<Button Click="Button_Click" DockPanel.Dock="Top" >
<Button.Content>
<DockPanel>
<TextBlock Text="{Binding title}" TextWrapping="Wrap" DockPanel.Dock="Top" Padding="5" HorizontalAlignment="Center" Foreground="#FFB51414" />
<l:ScrollViewerEx VerticalScrollBarVisibility="Auto" >
<TextBlock Text="{Binding description}" DockPanel.Dock="Top" TextWrapping="Wrap" Padding="5" IsHitTestVisible="False" />
</l:ScrollViewerEx>
</DockPanel>
</Button.Content>
</Button>
</DockPanel>
</GroupBox>
</DataTemplate>
In case, someone tried to resolve my previous question, I did it like the following:
DataTemplate mycolumnDataTemplate = null;
var dataTemplateStream = new SomeClass().GetType().Assembly.GetManifestResourceStream("Some.Namespace.SomeReosurceName.xaml");
string dataTemplateString = new System.IO.StreamReader(dataTemplateStream).ReadToEnd();
dataTemplateString = dataTemplateString.Replace("[0]", browserColumn.ColumnName);
mycolumnDataTemplate = XamlReader.Load(dataTemplateString) as DataTemplate;
What are you trying to achieve? Do you want the GroupBox Height to be changed at the runtime of your application, when some event occurred or some data has changed? If so, then what you are probably looking for is a data trigger or event trigger, which you simply need to add to your DataTemplate.

Categories

Resources