【问题标题】:ElementName Binding In ToolTip工具提示中的 ElementName 绑定
【发布时间】:2014-11-03 12:54:25
【问题描述】:

我有一个 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,但它似乎有点复杂。是否有一个简单的解决方案可以满足我的需求?

【问题讨论】:

  • 我假设 SearchResultsPresenter 是你的 GridView,如果你是 GridView,我想你会想要 Children.Count(但这会导致 a memory leak)你想要绑定到绑定路径填充网格视图以获取您的计数。可能会分享更多代码。
  • 谢谢。是的,SearchResultsPresenter 是我的 GridView(实际上是来自 Telerik 的 RadGridView),其属性为 ItemsSource="{Binding SearchResults}"。我可以在 ToolTip 中使用此 Binding 而不是 Items.Count,但如何显示 SelectedItems.Count ?

标签: xaml silverlight data-binding tooltip


【解决方案1】:
<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 ) );
}

【讨论】:

  • 我没办法。我是 Silverlight 书呆子 ;)
  • 这句话你现在不常听到了,哈哈,但是是的,我也是。
  • 很遗憾,Silverlight 是一个不错的框架。在与 Silverlight 合作了几年之后,我想我想出了“如何在 Silverlight 中正确地做事”,这种方式得到了框架的充分支持,并且可能被视为“创造者的意图”。唉!我现在正在做越来越多的 HTML 5,包括 AngularJS 和 TypeScript……我又是个初学者了。
  • 哈哈 我同意你必须深入研究 HTML5 的东西,我们仍在开发和支持一个非常大的 SL5 解决方案,同时向它迁移。这是一种耻辱,因为老实说,我对 XAML 的欣赏远胜于对 HTML5 的欣赏,更不用说跨浏览器/跨设备的开发对@$$ 来说是一个真正的痛苦。所以阿们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-04
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
  • 2015-09-02
相关资源
最近更新 更多