Привязка данных содержимого метки WPF - PullRequest
0 голосов
/ 02 ноября 2010

У меня есть метка в 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;
}

}

Ответы [ 2 ]

0 голосов
/ 03 ноября 2010

Попробовав способы связать свойства из объекта класса, не имея свойств зависимости и не повезло, я сделал следующее, и это работает как чудо!* и код 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 голосов
/ 02 ноября 2010

Вы должны реализовать INotifyPropertyChanged в 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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...