WPF Tooltip binding and open programatically does not work - wpf

I have a Custom tooltip icon which also has its content bound to Datacontext. My need was to open Tooltop on mouse hover as well as on click event. So I used following code
<Image Source="/MainUI;component\Images\home\tooltip_info.png"
Width="24" Height="24" Stretch="Fill" HorizontalAlignment="Right"
Name="ImageToolTip"
Margin="0,0,0,0" MouseUp="ImageToolTip_MouseUp" MouseLeave="ImageToolTip_MouseLeave">
<Image.ToolTip>
<ToolTip BorderBrush="Transparent" Background="Transparent" HorizontalOffset="-142">
<TextBlock TextWrapping="WrapWithOverflow"
Style="{StaticResource ExcalFont-MSFD300}"
FontSize="14" Text="{Binding Tips}"
Width="300" Padding="15,15,15,15">
<TextBlock.Background>
<ImageBrush ImageSource="Images\home\popupbox.png" />
</TextBlock.Background>
</TextBlock>
</ToolTip>
</Image.ToolTip>
</Image>
Code Behind:
private void ImageToolTip_MouseUp(object sender, MouseButtonEventArgs e)
{
((ToolTip)((FrameworkElement)sender).ToolTip).IsOpen = true;
}
private void ImageToolTip_MouseLeave(object sender, MouseEventArgs e)
{
((ToolTip)((FrameworkElement)sender).ToolTip).IsOpen = false;
}
Now the issue is on mouse up It opens Tooltip but it does not bind the text.
This is working fine if I am using static text instead of Binding. What am I doing wrong?
In case I mouse hover on icon then it works fine and shows the content too. Thereafter everytime mouse click also shows the tooltip content. Not sure why mouse click do not work initially. –

ToolTip is not part of the VisualTree.
A problem that is similar to the problem you have is described here:
RelativeSource binding from a ToolTip or ContextMenu
One option to solve your problem would be something like this:
<Image Source="/MainUI;component\Images\home\tooltip_info.png"
Width="24" Height="24" Stretch="Fill" HorizontalAlignment="Left"
Name="ImageToolTip"
Margin="0,0,0,0" MouseUp="ImageToolTip_MouseUp" MouseLeave="ImageToolTip_MouseLeave" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}">
<Image.ToolTip>
<ToolTip BorderBrush="Transparent" Background="Transparent" HorizontalOffset="-142" >
<TextBlock TextWrapping="WrapWithOverflow"
FontSize="14" Text="{Binding PlacementTarget.Tag.Tips,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ToolTip}}"
Width="300" Padding="15,15,15,15">
<TextBlock.Background>
<ImageBrush ImageSource="Images\home\popupbox.png" />
</TextBlock.Background>
</TextBlock>
</ToolTip>
</Image.ToolTip>
</Image>

Related

C# WPF MaterialDesign In Xaml Toolkit DialogHost. The background window behind the dialog box is not darkened

The background window is not darkened when DialogHost is called, meaning the dialog is not modal. The back window remains active. Although in the Github example works correctly. I don't know where I'm going.
XAML:
<materialDesign:DialogHost x:Name="DH_getLoadingList"
HorizontalAlignment="Center"
VerticalAlignment="Center"
CloseOnClickAway="True"
OverlayBackground="{DynamicResource PrimaryHueDarkBrush}" DialogTheme="Inherit" Grid.RowSpan="5" Grid.ColumnSpan="5">
<materialDesign:DialogHost.DialogContent>
<StackPanel Margin="16" Orientation="Vertical">
<Label Content="The row will be deleted." FontSize="16" />
<StackPanel Orientation="Horizontal">
<Button Content="OK" Style="{DynamicResource MaterialDesignFlatButton}" IsDefault="True" Margin="0,8,8,0" />
<Button Content="Cancel" Style="{DynamicResource MaterialDesignFlatButton}" Margin="0,8,8,0" />
</StackPanel>
</StackPanel>
</materialDesign:DialogHost.DialogContent>
</materialDesign:DialogHost>
C# code:
private void GettingFuelListCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
DH_getLoadingList.IsOpen = true;
}
UPDATED
I just had a misunderstanding the DialogHost structure. The correct way is
<Window>
<materialDesign:DialogHost>
<materialDesign:DialogHost.DialogContent>
<--! dialog content ... -->
</materialDesign:DialogHost.DialogContent>
<--! the correct location of the page content ... -->
</materialDesign:DialogHost>
<--! incorrect location of the page content ... -->
<Window/>
I know this is more than a year late. But when you put these properties:
HorizontalAlignment="Center"
VerticalAlignment="Center"
The overlay background will not appear, if you remove these, the problem will go away. The dialog will always be centered so don't need to worry about it's relative position to it's parent container.

WPF Popup disappear on Custom contextmenu opening

I have a TextBox in a PopUp. On clicking a ToggleButton, the PopUp Window opens.
Toggle Button Code
<ToggleButton Name="popupButton" Content="Click Me To Open Popup!" >
<ToggleButton.Template>
<ControlTemplate TargetType="ToggleButton">
<Border Name="bg" RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased" Background="Transparent" Width="17" Height="15">
<StackPanel HorizontalAlignment="Right" Width="17" >
<Path RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" Data="M186,52 C208,52 230,52 252,52" Fill="Black" Margin="0,1.5" Stretch="Fill" Stroke="#457faa" StrokeThickness="2" />
<Path RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" Data="M186,52 C208,52 230,52 252,52" Fill="Black" Margin="0,1.5" Stretch="Fill" Stroke="#457faa" StrokeThickness="2" />
<Path RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" Data="M186,52 C208,52 230,52 252,52" Fill="Black" Margin="0,1.5" Stretch="Fill" Stroke="#457faa" StrokeThickness="2" />
</StackPanel>
</Border>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
On clicking the ToggleButton it will open following the PopUp Window which contains the TextBox.
Popup Window Code
<Popup Name="testPopup" Placement="Bottom" PlacementTarget="{Binding ElementName=popupButton}" IsOpen="{Binding IsChecked, ElementName=popupButton}" StaysOpen="False">
<StackPanel Orientation="Horizontal">
<Label Content="Username: " Background="White"/>
<TextBox Text="Please RightClick to copy the text." ContextMenuOpening="TextBox_ContextMenuOpening">
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Copy"/>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</StackPanel>
</Popup>
When a user clicks on the TextBox in the PopUp I want to call the ContextMenu opening event which is as following:
CodeBehind for ContextMenuOpening Event
private void OpenContextMenu(FrameworkElement e)
{
if (e.ContextMenu != null)
{
e.ContextMenu.IsOpen = true;
}
}
private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
e.Handled = true;
this.OpenContextMenu(sender as FrameworkElement);
}
Purpose to Call ContextMenuOpening
I'm calling its opening event because I want to stop the parent contextmenu to open by e.Handled = true. Then I want to open the ContextMenu of the TextBox (source element). Here comes the issue.
Issue
As soon as I open the ContextMenu from code-behind, it opens the ContextMenu but the PopUp disappears as StayOpen property is set to False. I want to use StayOpen to False, as it should close as soon as the focus is lost.

ListBox with "load more" option

I would like to know how to construct the ListBox in WP7 that only load 20 items at a single time, and have a footer that show "load more" if there is any.
When user press the "load more", it will load another 20 in the list without loading the previous loaded data?
I am using LINQ at the behind source.
my code for XMAL as follow:
<Grid>
<ListBox name="newsIndexListBoxEN">
<ListBoxItem>
<DataTemplate>
<StackPanel Width="410" Orientation="Horizontal" VerticalAlignment="Top" Margin="0,5,0,5">
<StackPanel Background="DarkBlue" Margin="10,0,0,0" Height="100" Width="100" VerticalAlignment="Top">
<TextBlock Name="columnsTypeTB" Text="{Binding pType}" Margin="0,0,0,0" Foreground="White" FontSize="23" HorizontalAlignment="Center" />
<Image Width="100" Height="100" VerticalAlignment="Top" HorizontalAlignment="Center" Source="Background.png" />
</StackPanel>
<StackPanel Width="300" Height="100" Margin="0,0,0,0">
<Path Margin="0,0,0,0" Data="M39,8 L389,8" Fill="DarkBlue" Height="1" Stretch="Fill" Stroke="DarkBlue" UseLayoutRounding="False" Width="400"/>
<TextBlock Margin="8,0,0,0" Text="{Binding pTitle}" Tag="{Binding pID}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" Width="292" Height="66" />
<TextBlock Margin="8,5,0,0" Text="{Binding pDate}" Tag="{Binding pID}" MouseEnter="NewsViewContent_mouseEnter" Style="{StaticResource PhoneTextSmallStyle}" VerticalAlignment="Bottom" TextWrapping="Wrap" Width="292" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBoxItem>
</ListBox>
</Grid>
C# Code as follow:
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fs = storage.OpenFile(fileName, FileMode.Open))
{
XDocument menuIndex = XDocument.Load(fs);
var menuIndexList = from query in menuIndex.Descendants("news")
orderby (int)query.Element("newsID") descending
select new mkmenu
{
pID = query.Element("newsID").Value,
pTitle = query.Element("newsTitle").Value,
pDate = query.Element("newspDate").Value,
pType = newsType
};
newsIndexListBoxEN = menuIndexList.Count();
}
}
any ideas? sample code?
You can edit your listbox template to show a "Load more" button at the end of the list. In Blend, right click on your listbox, choose Edit Template, Edit a copy. By default, your listbox has a template like this:
ScrollViewer
ItemPresenter
Wrap your ItemPresenter into a StackPanel, then add a button at the end:
ScrollViewer
StackPanel
ItemPresenter
Button
That button will always be displayed at the end of the listbox. Handle the Clicked event of that button to add items to your ObservableCollection.
You can bind your listbox to ObservableCollection and add first 20 items on your page(app) load. Than after pressing "load more" get next 20 items and add to collection. Items will automatically be added to listbox.

WPF Image Tooltip

I have a tooltip on an image inside of a listbox. The tooltip is setup as follows:
<Image Grid.Column="0" Source="{Binding PingRankImage}"
Width="16" Height="16"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Image.ToolTip>
<ToolTip Content="{Binding Ping, StringFormat='Ping: {0}ms'}"
ContentStringFormat="{}Ping: {0}ms}" />
</Image.ToolTip>
</Image>
but the tooltip just displays the value and not the 'Ping: XXXms'
Any ideas?
You don't need extra {} prefix in ContentStringFormat. With ToolTip, also prefer using ContentStringFormat instead of StringFormat in binding.
Following works:
<Image.ToolTip>
<ToolTip Content="{Binding}"
ContentStringFormat="Ping: {0}ms" />
</Image.ToolTip>

Change content of ImageButton programatically (WPF)

I am using a ImageButton as
<Button Width="80" Height="25"
VerticalAlignment="Top"
HorizontalAlignment="Right"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Margin="0,0,1.5,0"
Name="btnActivate"
Click="OnActivate">
<StackPanel Orientation="Horizontal" Margin="3">
<Image Source="Shutdown.ico"
Width="12" Height="12"
Margin="0,0,5,0"/>
<TextBlock>Activate</TextBlock>
</StackPanel>
</Button>
I want to change the Content of button to 'Deactivate' when I click on it without changing the image. How can I do that? And also I need to some operations based on the content of button in my C# code.
You could just add a name to the TextBlock
<TextBlock Name="textBlock1">Activate</TextBlock>
and use it to modify the content in your OnActivate event handler
textBlock1.Text = "Deactivate";

Resources