2014-01-23 39 views
0

我正在製作一個基本程序,當用戶在文本框中輸入內容時,標籤會更新。我試圖使用數據綁定和INotifyPropertyChanged解決這個問題,所以我不想要任何解決方法。我使用了2個按鈕,所以我實際上可以看到他們是否更新。這裏是我的主類無法從另一個類中的wpf獲取標籤

namespace TestStringChangeFromAnotherClass 

public partial class MainWindow : Window 
{ 

    textClass someTextClass = new textClass(); 
    public MainWindow() 
    { 

     InitializeComponent(); 

    } 

    public string someString1; 
    public string someString2; 

    private void btn1_Click(object sender, RoutedEventArgs e) 
    { 
     someTextClass.Text1 = tbx1.Text; 
    } 

    private void btn2_Click(object sender, RoutedEventArgs e) 
    { 
     someTextClass.Text2 = tbx1.Text; 
    } 
} 

這裏的WPF它

<Window x:Class="TestStringChangeFromAnotherClass.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    Title="MainWindow" Height="350" Width="525"> 

    <Grid> 
    <Button x:Name="btn1" Content="Button" HorizontalAlignment="Left" Height="36" Margin="29,246,0,0" VerticalAlignment="Top" Width="108" Click="btn1_Click"/> 
    <Button x:Name="btn2" Content="Button" HorizontalAlignment="Left" Height="36" Margin="227,246,0,0" VerticalAlignment="Top" Width="124" Click="btn2_Click"/> 
    <Label x:Name="lbl1" Content="{Binding textClass.Text1}" HorizontalAlignment="Left" Height="37" Margin="74,32,0,0" VerticalAlignment="Top" Width="153"/> 
    <Label x:Name="lbl2" Content="{Binding textClass.Text2, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="38" Margin="74,90,0,0" VerticalAlignment="Top" Width="153"/> 
    <TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="37" Margin="290,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="190"/> 

</Grid> 

,你可以看到,我已經使用UpdateSourceTrigger嘗試。我也嘗試使用「someTestClass.Text1」而不是textClass.Test1,因爲這就是我在MainWindow中定義它的方式。這裏是我的textClass

namespace TestStringChangeFromAnotherClass 
public class textClass:INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private string text1; 
    public string Text1 
    { 
     get { return text1; } 
     set 
     { 
      text1 = value; 
      NotifyPropertyChanged("Text1"); 
     } 
    } 

    private string text2; 
    public string Text2 
    { 
     get { return text2; } 
     set 
     { 
      text2 = value; 
      NotifyPropertyChanged("Text2"); 
     } 
    } 

    protected void NotifyPropertyChanged(string info) 
    { 

     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 

我無法弄清楚如何讓WPF來尋找的Test1 Test2的或字符串在單獨的類和更新他們當琴絃改變。我有一個感覺問題在於DataContext,但我無法弄清楚。我也寧願不使用C#中的DataContext,只在WPF中

更新: 當我調試這個,當它到達NotifyPropertyChanged,PropertyChanged被評估爲null。這可能是問題嗎?

+0

你有沒有嘗試在Mainwindow中實現INotifyPropertyChanged呢?因爲這是你的數據上下文。 – TylerD87

+0

如果我在MainWindow中添加INotifyPropertyChanged,我得到這個錯誤-'TestStringChangeFromAnotherClass。MainWindow'沒有實現接口成員'System.ComponentModel.INotifyPropertyChanged.PropertyChanged' – darthwillard

+0

也許,我需要做的是嘗試讓UpdateSourceTrigger查看TextClass類中的PropertyChanged項目? – darthwillard

回答

1

您將DataContext綁定到您的Window,據我所知,它沒有textClass屬性。它有someTextClass字段textClass類型。爲了使您的代碼工作,你可以改變someTextClass公共財產:

public textClass someTextClass { get; private set; } 

初始化它在構造函數中:

public MainWindow() 
{ 
    someTextClass = new textClass(); 
    InitializeComponent(); 

} 

,然後更改綁定指向someTextClass財產

<Label x:Name="lbl1" Content="{Binding someTextClass.Text1}" .../> 
<Label x:Name="lbl2" Content="{Binding someTextClass.Text2}" .../> 
+0

將初始化移入構造函數並更改了綁定屬性,也將所有屬性更改爲public,不起作用 – darthwillard

+0

您的代碼與我的示例中的完全相同嗎?在我發佈答案之前,我已經試過這段代碼,它現在可以正常工作 – dkozl

+0

。奇怪的。我有它初始化InitializeComponent後。我必須在InitializeComponent之前初始化它?這是爲什麼?和「獲得私人設置」是什麼?做? – darthwillard

0

您與MainWindow類本身綁定爲DataContext,並嘗試訪問名爲的屬性具有要綁定到的屬性。

您正在運行到兩個問題:

1)您的XAML正試圖通過它的類型來引用所需的對象,而不是它的名字。不工作。你的綁定表達式應該看起來像{Binding someTextClass.Text1}(注意路徑表達式的第一部分的區別)。

2)你只能綁定到公共事物。你的領域沒有定義爲公開的,因此是私人的。即使XAML應邏輯地「能夠看到」屬性,因爲它是相同的類,DataBinding只能在公共屬性上工作。

3)編輯:你也必須使這個屬性。 WPF不會綁定到字段。

通常,使用Snoop將有助於診斷無提示綁定錯誤。

+0

1.我改變了它的綁定,不工作。我把所有事情都變成了公開的。那也沒用。 3.不確定你的意思嗎? – darthwillard

+0

而不是'textClass someTextClass = new textClass();',而是使用'public textClass someTextClass {get;在你的構造函數中,添加'someTextClass = new textClass();'[字段和C#中的屬性之間的區別是什麼?](http://stackoverflow.com/questions/295104/what-是字段和屬性之間的區別)可能有助於進一步解釋字段和屬性。 –

相關問題