【问题标题】:Translating Binding from C# to XAML将绑定从 C# 转换为 XAML
【发布时间】:2010-08-18 13:37:54
【问题描述】:

我无法在 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}" />

Path 和 Source 是一样的,我的错在哪里?

谢谢罗伯

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

    您无法将其转换为具有完全相同属性的 XAML,因为无法直接引用 this.Info。但是,您可以通过设置 RelativeSource 来获得相同的结果:

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

    【讨论】:

    • 感谢您的回答,但我会使用其他解决方案。
    【解决方案2】:

    您需要设置 DataContext。

    你可以添加:

    DataContext = Info
    

    之后

    InitializeComponent();
    

    并将 XAML 更改为:

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

    【讨论】:

    • 如果他这样做了,UserControl 中的所有东西都会有 Info 作为 DataContext,并且将无法绑定到其他东西......
    • 所以我觉得这种绑定比较容易,因为在这个控件中不需要绑定到其他人
    【解决方案3】:

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

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

    其中ns 是定义 InfoPool 的命名空间的 xaml 别名。 无需以这种方式弄乱您的 DataContext。

    【讨论】:

      【解决方案4】:

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

      我有实现 INotifyChangedProperty 的单例类 (InfoPool.cs)。它用于提供应用范围的可绑定属性。 App.xaml 中的 ObjectDataProvider 使用此类。所以绑定一个属性非常“容易”

      InfoPool.cs(Singleton 代码来自http://csharpindepth.com/Articles/General/Singleton.aspx 5th Version。我将Instance Property改为GetInstance() Method,因为OPD需要一个Method)

      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>
      

      就是这样。请发表评论,因为我是新手 ;-)

      【讨论】:

        【解决方案5】:

        如果您将单例实例访问器保留为属性,则不需要 ObjectDataProvider。您可以直接使用静态实例。

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

        【讨论】:

          猜你喜欢
          • 2012-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-19
          相关资源
          最近更新 更多