2016-01-05 56 views
2

我对c#WPF有点新。 我一直在关注MVVM模式,一切都设置好了,我的代码似乎工作正常,但我面临的问题是,当我绑定xaml文件上的数据时,我收到的数据来自set属性,但绑定似乎已经没有了数据显示在我的文本框中。检查我的代码。c#wpf数据绑定没有发生。

/********************** xaml code ********************* ************** \

<UserControl x:Class="ILS.debugger.debuggermsg" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:serial="clr-namespace:ILS.VM.Serial_Monitor;assembly=ILS.VM" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <TextBox Text="{Binding Debugger_Recoreded}" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Background="#FFEBD3D3"> 

     </TextBox> 
    </Grid> 
</UserControl> 

/*********************** viewmodel code ** **************** \

namespace ILS.VM.Serial_Monitor 
{ 
    public class serial : NotifyPropertyChanged 
    { 
     private string debuger_rec; 
     public string Debugger_Recoreded 
     { 
      get { return debuger_rec; } 
      set 
      { 

       if (this.debuger_rec == value) 
        return; 

       this.debuger_rec = value; 
       i--; 
       if (i == 0) 
       { 
        this.debuger_rec = String.Empty; 

        i = 1000; 
       } 
       this.InvokePropertyChanged("Debugger_Recoreded"); 

      }  

     } 

/*********************** model * ***************** \ 命名空间ILS

public void OnDebugger(String Receved_Data) //debug message monitor code 
     { 
      try 
      { 

       this.serialData.Debugger_Recoreded += " " + DateTime.Now + " " + Receved_Data + Environment.NewLine; 
       this.serialData.Debugger_Recoreded += Environment.NewLine; 
      } 
      catch (Exception e) 
      { 
      } 
     } 
+0

你设置了DataContext吗? – FatemehEbrahimiNik

回答

0
public class serial : INotifyPropertyChanged 
    { 
     private string debuger_rec; 
     public string Debugger_Recoreded 
     { 
      get { return debuger_rec; } 
      set 
      { 

       if (this.debuger_rec == value) 
        return; 

       this.debuger_rec = value; 
       i--; 
       if (i == 0) 
       { 
        this.debuger_rec = String.Empty; 

        i = 1000; 
       } 
       OnPropertyChanged("Debugger_Recoreded"); 

      }  


    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string name) 
      { 
       PropertyChangedEventHandler handler = PropertyChanged; 
       if (handler != null) 
       { 
        handler(this, new PropertyChangedEventArgs(name)); 
       } 
     } 
     } 

而设置的DataContext也是如此,在主窗口中输入该行:

this.DataContext = serialData; 

您也可以使用模式的方式进行绑定。

<TextBox Text="{Binding Debugger_Recoreded,Mode="Towway"}" /> 
+0

谢谢你的回复,我很抱歉,但我仍然面临同样的结果。 – anilG

+0

this.DataContext = serialData;使用此设置datacontext – FatemehEbrahimiNik

0

在您的代码隐藏(即你的debuggermsg类),你必须实例化并分配一个DataContext:

public debuggermsg() 
{ 
    InitializeComponent(); 
    this.DataContext = new serial(); 
} 

时需要使用数据绑定,这样你就能够与互动您的ViewModel的属性。

然后,修改您的视图模型,如下所示:需要

public class serial : INotifyPropertyChanged 
{ 
    private string debuger_rec; 
    public string Debugger_Recoreded 
    { 
     get { return debuger_rec; } 
     set 
     { 
      if (this.debuger_rec == value) 
       return; 

      this.debuger_rec = value; 
      i--; 
      if (i == 0) 
      { 
       this.debuger_rec = String.Empty; 

       i = 1000; 
      } 
      OnPropertyChanged("Debugger_Recoreded"); 
     }  
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 

OnPropertyChanged的实现方法来通知你的视图模型的属性的修改的视图。

一切都应该罚款然后。

0

在实施INotifyPropertyChanged,最好使用[CallerMemberName]属性,它在System.Runtime.CompilerServices,因为你不必硬编码调用属性的字符串名称:

 public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged([CallerMemberName]string propertyName = "") 
     { 
      var handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     }   

现在你可以写你的财产是这样:

private string debuggerRecorded; 
public string DebuggerRecorded 
{ 
    get 
    { 
     return debuggerRecorded; 
    } 
    set 
    { 
     if (debuggerRecorded != value) 
     { 
      this.debuggerRecorded = value; 
      i--; 
      if (i == 0) 
      { 
       this.debuggerRecorded = String.Empty; 
       i = 1000; 
      } 
      OnPropertyChanged(); // No argument needed 
     }  
    } 
} 

这样做,你不必担心拼写,你可以自由地在未来改变你的属性的名称,你不必记得也改变它在OnPropertyChanged

假设一切正常,你的代码你只需要设置DataContext,这通常在MainWindow中完成。例如,像这样:

public partial class MainWindow : Window 
    { 
     private Serial viewModel; 
     public MainWindow() 
     { 
      InitializeComponent(); 
      viewModel = new Serial(); 
      this.DataContext = viewModel; 
     } 
    } 

此外,您可能希望与另一个属性写你的文本框:

TextBox Text="{Binding DebuggerRecorded, UpdateSourceTrigger=PropertyChanged}" ...

如果忽略这最后一部分,Text将只得到时更新TextBox失去了重点。

+0

谢谢文森特。我会尝试这个并回复你。 – anilG

+0

@anilG(因为我没有足够的声誉,所以我不得不在这里发表评论)你有没有可能在你的模型中实例化你的串行类? “serialData”对象?你的模型中的OnDebugger(String Receved_Data)方法是否改变了视图模型的属性,或者你是否只有一些其他模型,它们的属性名称与序列的属性相同? –