在按钮工具提示上设置绑定,并将声明的用户控件实例作为源

时间:2014-05-08 03:45:54

标签: wpf binding user-controls tooltip

我在我的WPF项目中添加了一个用户控件(带有标签和文本框):

<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>

并在我的主窗口代码后面创建了该用户控件的实例。

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

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

在我的主窗口中,我有一个按钮列表,我想将他们的tooltip属性(或绑定tooltip属性)设置为我的用户控件的实例,以便在我向文本框添加值时工具提示动态更新我的用户控制。到目前为止,在我的主窗口xaml中,我已经能够将我的用户控件显示为此处显示的工具提示:

<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>

我仍然不清楚将工具提示设置为我的主窗口代码中声明的用户控件的实例。任何帮助都会有很长的路要走。感谢。

1 个答案:

答案 0 :(得分:0)

如果您在后台设置工具提示,请不要使用以下代码:

<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>

如果您希望工具提示动态更新:

private myControl mytooltip = new myControl();

    public MainWindow()
    {
        InitializeComponent();
        AddStackPanelChildren();
        mytooltip._TextboxUrl.Text = "Hello";
        _ButtonEPSSettings.ToolTip = mytooltip;
        //SetButtonTootips();
    }