2010-08-18 123 views
3

我无法在XAML中获得此绑定。将C#绑定到XAML

绑定在C#中的作品:

public partial class myControl : UserControl 
{ 
    // get singleton instance 
    InfoPool Info = InfoPool.Info; 

    public myControl() 
    { 
     InitializeComponent(); 

     // Test Binding 
     Binding bind = new Binding(); 
     bind.Source = this.Info; 
     bind.Path = new PropertyPath("Time"); 
     txtInfoTime.SetBinding(TextBlock.TextProperty, bind); 
    } 
} 

在XAML没有约束力:

<TextBlock x:Name="txtInfoTime" Text="{Binding Path=Time, Source=Info}" /> 

路径和来源是一样的,所以这里是我的错?

THX罗布

回答

2

您需要设置DataContext。

您可以再补充:

DataContext = Info 

InitializeComponent(); 

并更改XAML到:

<TextBlock x:Name="txtInfoTime" Text="{Binding Time}" /> 
+2

如果他这样做,UserControl中的一切都将Info作为DataContext,并且将无法绑定到其他东西... – 2010-08-18 13:48:22

+0

很高兴知道。感谢:) – Crispy 2010-08-18 13:50:08

+0

所以这种绑定更容易我认为,在这个控制中不需要绑定到其他人 – 2010-08-18 14:38:09

4

你无法将它与完全相同的特性,为XAML,因为没有办法直接引用this.Info。但是,通过设置RelativeSource可以达到相同的结果:

<TextBlock x:Name="txtInfoTime" Text="{Binding Path=Info.Time, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:myControl}}}" /> 
+0

感谢您的回答,但我会使用其他解决方案。 – 2010-08-18 14:39:12

1

由于InfoPool是单一实例,我会建议如下:

<TextBlock x:Name="txtInfoTime" Text="{Binding Path=Time, Source={x:Static ns:InfoPool.Info}}"/> 

其中ns是定义InfoPool的名称空间的xaml别名。不需要这样搞乱你的DataContext。

1

根据你的回答,我解决了如下所述的问题。我发布它,因为我猜想WPF中的其他初学者可能会发现它很有用。

我有singleton类(InfoPool.cs)实现INotifyChangedProperty。它用于提供应用程序中可绑定的属性。该类由App.xaml中的ObjectDataProvider使用。所以这是非常“简单”绑定到一个属性

InfoPool.cs(Singleton的代码是从http://csharpindepth.com/Articles/General/Singleton.aspx第5版。我改变了实例属性来getInstance()方法,因为OPD需要一个方法)

public sealed class InfoPool : INotifyPropertyChanged 
{ 
    InfoPool() 
    { 
    } 

    public static InfoPool GetInstance() 
    { 
     return Nested.instance; 
    } 

    class Nested 
    { 
     static Nested() 
     { 
     } 

     internal static readonly InfoPool instance = new InfoPool(); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(PropertyChangedEventArgs e) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, e); 
     } 
    } 

    private String strProp = "default String"; 
    private Double dblProp = 1.23456; 

    public String StrProp 
    { 
     get { return strProp; } 
     set 
     { 
      strProp = value; 
      OnPropertyChanged(new PropertyChangedEventArgs("StrProp")); 
     } 
    } 

    public Double DblProp 
    { 
     get { return dblProp; } 
     set 
     { 
      dblProp = value; 
      OnPropertyChanged(new PropertyChangedEventArgs("DblProp")); 
     } 
    } 

在App.xaml中的ObjectDataProvider的

<Application.Resources> 
    <ResourceDictionary> 
     <ObjectDataProvider x:Key="OPDInfo" ObjectType="{x:Type local:InfoPool}" MethodName="GetInstance" d:IsDataSource="True"/> 
    </ResourceDictionary> 
</Application.Resources> 

的下面是绑定到OPD

<StackPanel> 
    <TextBlock x:Name="tbprop1" Text="{Binding Path=StrProp, Source={StaticResource OPDInfo}}" /> 
    <TextBlock x:Name="tbprop2" Text="{Binding DblProp}" ToolTip="{Binding StrProp}" DataContext="{StaticResource OPDInfo}" /> 
</StackPanel> 
种方式

就是这样。请评论,因为我是新来的;-)

1

如果你离开你的单身实例访问器作为一个属性,你不需要一个ObjectDataProvider。你可以直接使用静态实例。

<StackPanel> 
<TextBlock x:Name="tbprop1" 
    Text="{Binding Path=StrProp, Source={x:Static local:InfoPool.Instance}}" /> 
</StackPanel> 
+0

只需注意到BubbleWrap已经给出了这个信息。 – grantnz 2010-08-26 04:39:55