c#wpf数据绑定没有发生。

时间:2016-01-05 05:26:06

标签: c# wpf

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

/ ********************** xaml代码********************** ************* \

<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");   

           }    

        }

/ ********模型****************** \ 命名空间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)
            {
            }
        }

3 个答案:

答案 0 :(得分: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"}" />

答案 1 :(得分:0)

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

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

DataBinding是必需的,因此您可以与ViewModel的属性进行交互。

然后,像这样修改您的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方法来通知您对ViewModel属性的修改视图。

一切都应该没问题。

答案 2 :(得分: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失去焦点时才会更新。

相关问题