Question

I've added a user control (with labels and textboxes) to my WPF project shown here:

<UserControl x:Class="MyProject.myControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="384" Width="543"> <Grid x:Name="_LabelServerURL"> <Label x:Name="_LabelServerUrl" Content="Server Url:" HorizontalAlignment="Left" Margin="60,21,0,0" VerticalAlignment="Top" FontWeight="Bold"/> <TextBox x:Name="_TextboxUrl" HorizontalAlignment="Left" Height="23" Margin="158,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="235"/> <Label x:Name="_LabelURLExample" Content="(https://promodel.com)" HorizontalAlignment="Left" Margin="398,21,0,0" VerticalAlignment="Top" Width="139"/> </Grid> </UserControl>

and have created an instance of that user control in my main window code behind.

  public partial class MainWindow : Window
{
    private myControl _settings = new myControl();

    public MainWindow()
    {
        InitializeComponent();
        AddStackPanelChildren();
        _ButtonEPSSettings.ToolTip = _epsSettings;
        //SetButtonTootips();
    }

In my main window I have a list of buttons, and I want to set their tooltip property (or bind the tooltip property) to the instance of my user control so that the tooltip dynamically updates when I add values to the textboxes in my user control. So far in my main window xaml I've been able to show my user control as the tooltip shown here:

<Window x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls ="clr-namespace:MyProject" <!--Declare project namespace-->
    Title="EPS Configuration Manager" Height="415" Width="766">
<Grid>
    <Button x:Name="_ButtonSettings" 
            Content="EPS Settings" 
            HorizontalAlignment="Left" 
            Margin="10,10,0,0" 
            VerticalAlignment="Top" 
            Width="199" 
            HorizontalContentAlignment="Left"
            BorderThickness="0"  
            Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="_ButtonSettings_Click"
            >
        <Button.ToolTip>
            <ToolTip>
                <StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
                    <Controls:myControl/>
                </StackPanel>
            </ToolTip>
        </Button.ToolTip>
    </Button>

I am still not clear on setting that tooltip as the instance of my user control as declared in my main window code behind. Any help will go a long way. Thanks.

Was it helpful?

Solution

If you set tooltips in backgroud, don't use these code:

<Button.ToolTip>
            <ToolTip>
                <StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
                    <Controls:myControl/>
                </StackPanel>
            </ToolTip>
</Button.ToolTip>

If you want the tooltip dynamically updates:

private myControl mytooltip = new myControl();

    public MainWindow()
    {
        InitializeComponent();
        AddStackPanelChildren();
        mytooltip._TextboxUrl.Text = "Hello";
        _ButtonEPSSettings.ToolTip = mytooltip;
        //SetButtonTootips();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top