2010-12-01 57 views
4

我正在创建一个简单的用户控件;只是一个ImageButton。WPF - 自定义用户控件上的工具提示文本问题

我已经成功将图像绑定到按钮,所以我决定添加一个工具提示。现在我有麻烦了。看起来我可以在控件的XAML中对工具提示的文本进行硬编码,但是当它被绑定时它会返回一个空字符串。

下面是我的控制XAML:

<Button x:Class="BCOCB.DACMS.Controls.ImageButton" 
      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="300" d:DesignWidth="300" 
     Name="this" 
     Style="{StaticResource DisabledButton}"> 

    <Image Source="{Binding ElementName=this, Path=Source}" />  
    <Button.ToolTip> 
     <TextBlock Text="{Binding ElementName=this, Path=ToolTipText}" /> 
    </Button.ToolTip> 
</Button> 

而这里的工具提示文本的依赖项属性信息:

public static readonly DependencyProperty ToolTipTextProperty = DependencyProperty.Register("ToolTipText", typeof(string), typeof(ImageButton)); 
public string ToolTipText 
{ 
    get 
    { 
    return this.GetValue(ToolTipTextProperty) as string; 
    } 
    set 
    { 
    this.SetValue(ToolTipTextProperty, value); 
    } 
} 

最后,在我的窗口控制的声明:

<controls:ImageButton x:Name="btnAdd" Source="/DACMS;component/Resources/plus.png" ToolTipText="Add New Item" Click="btnAdd_Click" /> 

正如我之前提到的,图像绑定很好,我已经完全一样方式。

任何想法?

感谢,
桑尼

编辑:我现在的工作。我从绑定中删除了ElementName,并在Instanciation的后面的代码中设置了TextBlock的DataContext = this。不过,我想知道如何在XAML中解决这个问题。

回答

6

我无法现在来测试这个权利,但你可以尝试:

<Button.ToolTip 
     DataContext=”{Binding Path=PlacementTarget.Parent.Parent, 
        RelativeSource={x:Static RelativeSource.Self}}" 
> 
    <TextBlock Text="{Binding Path=ToolTipText}" /> 
</Button.ToolTip> 

您可能必须尝试一点点在PlacementTarget“父”的数量。

希望这能奏效。我不喜欢给出我没有测试过的答案,但是我没有在这台计算机上使用VS。 :)

+0

非常接近。我最终想到了这一点,但我会因为让我走上正确的道路而给你信心。 我最终将``改为`` – 2010-12-22 22:43:15

1

我有绑定到ContextMenu的同样的问题。在我的研究之后,我认为这是因为ToolTip和ContextMenu不存在于页面/窗口/控件的可视化树中。因此DataContext不会被继承,并且会使绑定变得麻烦。

这是我发现的Xaml黑客为我工作。

Binding to a MenuItem in a WPF Context Menu

0

由于您不改变任何东西,但工具提示TextBlock上的文本,你可以使用内联声明,它会为你生成TextBlock,并且不需要任何黑客来解决名称范围问题,重新运行到否则:

<Button ... ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=ToolTipText}">... 

你可以交替设置在图像的工具提示和使用控制为DataContext的,其周围的名称作用域问题得到。在DataContext将被传递给工具提示,允许正常的绑定:

<Image DataContext="{Binding ElementName=this}" Source="{Binding Source}"> 
    <Image.ToolTip> 
     <TextBlock FontSize="18" Text="{Binding Path=ToolTipText}" /> 
    </Image.ToolTip> 
</Image> 

这种方式允许在TextBlock的或更复杂的视觉效果,附加设置。

1

设置为数据上下文“这个”通过XAML看起来像这样的方式:

<Control DataContext={Binding RelativeSource={RelativeSource Self}}> 

至于另一点,WPF按钮允许他们的内容是几乎任何你想要的(单)的东西。如果你想文本以外的东西(例如,文本和图像),它看起来像这样:

<Button Name="SampleButton" Click="SampleButton_Click"> 
    <Grid Width="70" Height="62"> 
     <Label Content="SampleText"/> 
     <Image Margin="3,3,3,3" Source="Graphics/sample.ico"/> 
    </Grid> 
</Button> 
0

这解决了工具提示绑定和依赖关系属性的问题:

<UserControl x:Class="Extended.InputControls.TextBoxUserControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Extended.InputControls" 
    x:Name="UserControl" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
    <TextBox x:Name="textBox"> 
     <TextBox.ToolTip> 
      <ToolTip Content="{Binding Path=CustomToolTip}" Background="Yellow"/> 
     </TextBox.ToolTip> 
    </TextBox> 
</UserControl> 

取而代之的是(不工作):

<UserControl x:Class="Extended.InputControls.TextBoxUserControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Extended.InputControls" 
    x:Name="UserControl"> 
    <TextBox x:Name="textBox"> 
     <TextBox.ToolTip> 
      <ToolTip Content="{Binding ElementName=UserControl, Path=CustomToolTip}" Background="Yellow"/> 
     </TextBox.ToolTip> 
    </TextBox> 
</UserControl>