ToolTip中的ElementName绑定

时间:2014-11-03 12:54:26

标签: xaml silverlight data-binding tooltip

我有一个Silverlight应用程序。

我需要在一个文本块中添加一个工具提示,该文本块将显示有关其他元素(GridView)的信息

<ToolTipService.ToolTip>
    <ToolTip>
        <TextBlock x:Name="Test"
            VerticalAlignment="Center"
            FontSize="12"
            Margin="10,0,0,0"
            Foreground="DimGray"
            Visibility="Visible">
            <Run Text="{Binding Path=Items.Count,
                ElementName=SearchResultsPresenter,
                StringFormat=\{0:N0\}}"/>
            <Run Text="{Binding
                Source={StaticResource PublicResourceStrings},
                Path=ResourceStrings.SEARCH_RESULTS_DISPLAYED}"/>
            <Run Text="{Binding SelectedItems.Count,
                ElementName=SearchResultsPresenter,
                StringFormat=\{0:N0\}}"/>
            <Run Text="{Binding
                Source={StaticResource PublicResourceStrings},
                Path=ResourceStrings.SEARCH_RESULTS_SELECTED}"/>
        </TextBlock>
    </ToolTip>
</ToolTipService.ToolTip>

但是与elementName的绑定不起作用。 Items.Count和SelectedItems.Count显示“0”......

我找到this但似乎有点复杂。有没有一个简单的解决方案可以满足我的需求?

1 个答案:

答案 0 :(得分:2)

<Grid>
    <Grid.Resources>
        <BindableObjectReference x:Key="BindableGridView"
            Object="{Binding ElementName=SearchResultsPresenter}"/>
    </Grid.Resources>
    <RadGridView x:Name="SearchResultsPresenter"
        ItemsSource="{Binding SearchResults}">
        <ToolTipService.ToolTip>
            <ToolTip>
              <TextBlock>
                <Run Text="{Binding
                    Path=Object.Items.Count,
                    Source={StaticResource BindableGridView}}"/>
                <Run Text="{Binding
                    Path=Object.SelectedItems.Count,
                    Source={StaticResource BindableGridView}}"/>
              </TextBlock>
            <ToolTip>
        <ToolTipService.ToolTip>
    </RadGridView>
</Grid>

和代码:

public class BindableObjectReference : DependencyObject
{
    public object Object
    {
        get { return GetValue( ObjectProperty ); }
        set { SetValue( ObjectProperty, value ); }
    }

    public static readonly DependencyProperty ObjectProperty =
        DependencyProperty.Register( "Object", typeof( object ),
        typeof( BindableObjectReference ), new PropertyMetadata( null ) );
}