2010-11-02 128 views
0

我在WPF4中有一个标签,并尝试将内容绑定到来自c#类的值。我创建了一个ObjectDataProvider,但出于某种原因无法看到内容或更新。你能指点我做错了什么吗?WPF标签内容数据绑定

这里是XAML -

<Grid.Resources> 
<local:SummaryData x:Key="mySummaryData"/> </Grid.Resources> 
<Label Name ="lbCurrentVerValue" Grid.Row="1" Grid.Column="2" Content="{Binding Path=frameworkVersion, Source={StaticResource mySummaryData}}"/> 
<TextBox Name ="lbFromVerValue" Grid.Row="2" Grid.Column="2" Text="{Binding Source={StaticResource mySummaryData}, Path=frameworkVersion}"/> 

这里是C#代码 -

namespace DBUpgradeUI 

{

public partial class DBUpgReadinessCheck : Window 
{ 
    public string userConnStr = String.Empty; 
    public string userFoldPath = String.Empty; 
    public SummaryData sd = new SummaryData(); 

    public DBUpgReadinessCheck() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     ReadinessCheck(userConnStr, userFoldPath); 
    } 

    public void ReadinessCheck(string connectionString, string folderPath) 
    { 
     FrmImportUtility frmWork = new FrmImportUtility(); 
     sd.frameworkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); ; 
     frmWork.CurrentlyProcessingVersion(connectionString, folderPath, ref sd.currentVersion, ref sd.finalVersion); 
    } 
} 
public class SummaryData 
{ 
    public string currentVersion = "Test"; 
    public string finalVersion = "finalVerTest"; 
    public string frameworkVersion = String.Empty; 
} 

}

+0

太多的代码,你可以使用较小的示例程序重现此问题吗? – Kugel 2010-11-02 12:57:26

+0

由于XAML编译的方式,IIRC绑定到“内部”成员不起作用。你可以试着让这些成员公开,看看是否改变了事情? – 2010-11-02 13:38:13

+0

Kugel,只是对xaml进行编辑,以显示相关代码 – Nikhil 2010-11-02 13:39:38

回答

0

在尝试从类对象中绑定属性而不具有依赖项属性且没有运气的方法之后,我完成了以下操作,它的工作原理类似于魅力!

XAML -

<Label Name ="lbCurrentVerValue" Grid.Row="1" Grid.Column="2" Content="{Binding Mode=OneWay, ElementName=step2Window,Path=currentVersion}"/> 
    <Label Name ="lbFromVerValue" Grid.Row="2" Grid.Column="2" Content="{Binding Mode=OneWay, ElementName=step2Window,Path=finalVersion}"/> 

和C#代码 -

public partial class DBUpgReadinessCheck : Window 
{ 
    //Values being passed by step1 window 
    public string userConnStr = String.Empty; 
    public string userFoldPath = String.Empty; 

    //Dependency properties 
    public string currentVersion 
    { 
     get { return (String)this.GetValue(CurVersionProperty); } 
     set { this.SetValue(CurVersionProperty, value); } 
    } 
    public static readonly DependencyProperty CurVersionProperty = 
      DependencyProperty.Register("currentVersion", typeof(String), typeof(DBUpgReadinessCheck), new UIPropertyMetadata("0")); 


    public String finalVersion 
    { 
     get { return (String)GetValue(FinVerProperty); } 
     set { SetValue(FinVerProperty, value); } 
    } 
    public static readonly DependencyProperty FinVerProperty = 
     DependencyProperty.Register("finalVersion", typeof(String), typeof(DBUpgReadinessCheck), new UIPropertyMetadata("0")); 

    public string frameworkVersion 
    { 
     get { return (String)GetValue(FrameWrkVersionProperty); } 
     set { SetValue(FrameWrkVersionProperty, value); } 
    } 
    public static readonly DependencyProperty FrameWrkVersionProperty = 
     DependencyProperty.Register("frameworkVersion", typeof(String), typeof(DBUpgReadinessCheck), new UIPropertyMetadata("0")); 

    public DBUpgReadinessCheck() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     ReadinessCheck(userConnStr, userFoldPath); 
    } 

    public void ReadinessCheck(string connectionString, string folderPath) 
    { 
     FrmImportUtility frmWork = new FrmImportUtility(); 
     frameworkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 
     string tempCurVersion = string.Empty; 
     String tempFinalVersion = string.Empty; 
     frmWork.CurrentlyProcessingVersion(connectionString, folderPath, ref tempCurVersion, ref tempFinalVersion); 
     currentVersion = tempCurVersion; 
     finalVersion = tempFinalVersion; 

     //Validation 
     if (finalVersion == frameworkVersion) 
     { 
      string strUri2 = String.Format(@"pack://application:,,,/GreenCheck.jpg"); 
      ImgFrmVersion.Source = new BitmapImage(new Uri(strUri2)); 
      yelloExclamation.Visibility = System.Windows.Visibility.Hidden; 
      errMsg.Visibility = System.Windows.Visibility.Hidden; 
      succMsg.Visibility = System.Windows.Visibility.Visible; 
      btRecheckSetUp.Visibility = System.Windows.Visibility.Hidden; 
      btStartUpgrade.IsEnabled = true; 
     } 
    } 

这个伟大的工程。谢谢大家的帮助。

0

您必须SummaryData执行INotifyPropertyChanged:

public class SummaryData:INotifyPropertyChanged 
{ 
    private string currentVersion = "Test"; 

    public string CurrentVersion 
    { 
    get { return currentVersion; } 
    set { 
      currentVersion = value; 
      NotifyChanged("CurrentVersion"); 
     } 
    } 

    private void NotifyChanged(string p) 
    { 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, 
      new PropertyChangedEventArgs(property)); 
    } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
} 
+0

试过这个,但是价值仍然没有显示,即使是第一次价值是“测试” – Nikhil 2010-11-02 13:51:04

+0

任何人都可以指向正确的方向吗? – Nikhil 2010-11-02 19:00:31

+0

尝试使财产公开,而不是内部 – Andy 2010-11-03 07:32:55