WPF数据绑定问题 [英] Problem with WPF Data Binding

查看:73
本文介绍了WPF数据绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用WPF进行项目,对于整个绑定问题我有些困惑.我阅读了几篇有关的文章和示例,但似乎在我的应用程序中不起作用.
因此,基本上需要将Settings-Class的属性绑定到其他控件(文本框,复选框等).

设置类现在看起来像这样(以后会有更多属性):

Hi, I''m currently working on a project with WPF and I''m a bit confused about the whole binding thing. I read several articles and example about, but doesn''t seem to work in my application.
So what is basically want is binding the properties of a Settings-Class to a different controls (textboxes, checkboxes, etc.).

The Settings-Class looks like this now (there will be more properties later):

public class Settings
    {
        public Settings() {
            defaultConfig = new DefaultAdapterConfig() { Length = 300, Length2 = 300, IpAddress = "192.168.0.100", Port = 3000, Tool = new VirtualTool() };
        }

        DefaultAdapterConfig defaultConfig;

        public DefaultAdapterConfig DefaultConfig
        {
            get { return defaultConfig; }
            set { defaultConfig = value; }
        }

    }



而DefaultAdapterConfig看起来像这样:



And the DefaultAdapterConfig looks like this:

public class DefaultAdapterConfig : INotifyPropertyChanged
    {
        public const string NAME = "Default";

protected ITool tool;

        public ITool Tool
        {
            get { return tool; }
            set { tool = value;
            NotifyPropertyChanged("Tool");
            }
        }

        protected float length;

        public float Length
        {
            get { return length; }
            set
            {
                length = value;
                NotifyPropertyChanged("Length");
            }
        }

        protected float length2;

        public float Length2
        {
            get { return length2; }
            set
            {
                length2 = value;
                NotifyPropertyChanged("Length2");
            }
        }

        string ipAddress;

        public string IpAddress
        {
            get { return ipAddress; }
            set { ipAddress = value; 
                NotifyPropertyChanged("IpAddress");}
        }

        int port;

        public int Port
        {
            get { return port; }
            set { port = value; 
                NotifyPropertyChanged("Port");}
        }


        private void NotifyPropertyChanged(String property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }



我在窗口中有一个Settings对象的实例,从中获得了一个属性,现在我想使用以下代码绑定该对象的属性:



I have an instance of the Settings object in my window, made a property out of it and now I want to bind the properties of the object using the following code:

<TextBox Margin="5" Width="75" HorizontalAlignment="Left" Name="tbDALength" Text={Binding Settings.DefaultConfig.Length}></TextBox>



我的问题是,文本框不显示任何内容.您对问题可能是什么以及如何解决有任何建议吗?



My problem is, that the textbox doesn''t show any content. Do you have any suggestions what the problem might be and how to solve it?

推荐答案

您是否正在设置DataContext =< your instance =" of =" settings ="> ?
Are you setting your DataContext = <your instance="" of="" settings=""> ?


我将首先查看您是否首先绑定到DefaultConfig.覆盖DefaultAdapterConfig的ToString 方法,并尝试绑定到Settings.DefaultConfig.请记住,您必须将DataContext设置在某处.一个很好的起点是XAML背后的代码的构造函数.
I would start by seeing if you are binding to the DefaultConfig first. Override the ToString method for DefaultAdapterConfig, and attempt to bind to Settings.DefaultConfig. Remember that you do have to set your DataContext somewhere. A good place to start is in the constructor of the code behind the XAML.
public partial class MainView
{
  public MainView()
  {
    InitializeComponent();
    DataContext = new Settings()
  }
}


这篇关于WPF数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆