【问题标题】:Binding to another control in Silverlight绑定到 Silverlight 中的另一个控件
【发布时间】:2010-12-06 18:15:41
【问题描述】:

有没有办法绑定到另一个控件的实例?像这样的:

<Button x:Name="Foo" Content="Foo" />
<local:CustomControl OtherControl="{Binding Foo}" />

我尝试在 MainPage 的构造函数中将 DataContext 设置为“this”,但它似乎不起作用。

CustomControl 的定义如下:

class CustomControl
{
    public FrameworkElement OtherControl { get; set; }
}

【问题讨论】:

    标签: silverlight binding


    【解决方案1】:
    <Button x:Name="Foo" Content="Foo" />
    <local:CustomControl x:Name="control" OtherControl="{Binding ElementName=Foo}" />
    

    【讨论】:

    • 这可能不适用于 Silverlight 2.0,但对于 5.0(可能更低),这绝对是绑定另一个控件实例的正确答案。
    【解决方案2】:

    不确定您要做什么,但在 Silverlight 3 中,您可以使用元素绑定来绑定到控件上的属性。

    <Button x:Name="Foo" Content="Foo" />
    <local:CustomControl x:Name="control" Property="{Binding Path=Content, ElementName=Foo}" />
    

    在代码中,您总是可以分析绑定并从中获取元素?

    control.GetBindingExpression(Property).ParentBinding.Source

    【讨论】:

      【解决方案3】:

      在 Silverlight 2 中是不可能的:

      Silverlight 2 不允许您将一个元素绑定到另一个元素。相反,所有绑定都是数据 对象。 (您可以使用中间对象解决此问题,但增加的不便意味着 这很少值得。)

      【讨论】:

        猜你喜欢
        • 2010-10-16
        • 1970-01-01
        • 2011-11-14
        • 1970-01-01
        • 1970-01-01
        • 2014-06-14
        • 2012-01-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多