WPF 标签内容数据绑定

发布于 2024-09-30 10:29:47 字数 1578 浏览 0 评论 0原文

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

这是 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;
}

}

I have a label in WPF4 and trying to bind the content to a value from c# class. I have created an ObjectDataProvider but for some reason can't see the content or updates. Can you point me to what I'm doing wrong?

Here is the 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}"/>

and here is the c# code-

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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

居里长安 2024-10-07 10:29:47

您必须在 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
}

You must implement INotifyPropertyChanged in SummaryData:

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
}
鱼忆七猫命九 2024-10-07 10:29:47

在尝试了在没有依赖属性的情况下从类对象绑定属性并且没有运气的方法之后,我完成了以下操作,它的工作方式就像一个魅力!

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;
        }
    }

这很好用。感谢大家的帮助。

After trying ways to bind properties from a class object without having dependency properties and having no luck, I have done the following and it works like a charm!

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}"/>

and c# code -

 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;
        }
    }

This works great. Thanks everyone for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文