数据绑定问题 [英] Data Binding Problems

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

问题描述

这是我的C#代码:-

This is my C# code:-

namespace dynamic_inputs
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        List<string> _firstNo = new List<string>();
        int i=0;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }


        private string firstno;
        public string FirstNo
        {
            get
            {
                return firstno;                
            }
            set
            {
               if (_firstNo != null && _firstNo.Count != 0)
              {
                    
                    _firstNo.Add(value);
                    firstno = _firstNo[0];
                    
               }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            NotifyPropertyChange("FirstNo");
        }

        #region NotifyPropertyChanged

        /// <summary>
        /// Occurs when a property value changes
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raise the  <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="propertyName"></param>
        protected void NotifyPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }
}






这是我的Xaml






This is my Xaml

<Window x:Class="dynamic_inputs.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">
    <Grid HorizontalAlignment="Center"

          VerticalAlignment="Center" Height="316" Width="485">
        <Grid.RowDefinitions>
            <RowDefinition Height="79*" />
            <RowDefinition Height="66*" />
            <RowDefinition Height="92*" />
            <RowDefinition Height="79*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock Text="Name: " Margin="0,0,188,42" />
        <TextBox Text="{Binding FirstNo}" Name="asd" Grid.ColumnSpan="2" Margin="89,0,0,41" />
        <Button Grid.Row="1" Margin="179,0,200,27" 

                Content="Submit"

                Click="Button_Click" Grid.ColumnSpan="2" />
        <TextBlock Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="157,16,0,0" Name="textBlock1" Text="{Binding FirstNo}" VerticalAlignment="Top" Width="156" Grid.ColumnSpan="2" />
    </Grid>
</Window>








该名称未绑定到文本框.我只是一个初学者.有点困惑:/

谢谢:)








The name is not binding to the text box. I am just a beginner. so a little confused :/

Thanks :)

推荐答案

通常,首先创建两个单独的对象.原因是它更清洁并且遵循关注点分离.您所做的是在没有用途的实际UI类上实现INotifyPropertyChange的. UI类具有对其UI元素的完全访问权限,那么为什么要使用额外的事件(即PropertyChange)?


其次,您的设置器逻辑有些奇怪.您正在向集合中添加一个值,然后始终将返回"值设置为第一项.这意味着添加项目后它永远不会改变.

最后,您没有在设置器上发布属性更改.我可以通过单击按钮看到它,但是单击按钮不会执行任何其他操作.

不确定为什么要收藏,但尝试更简单的方法:

First off usually you make 2 separate objects. The reason being is it is cleaner and follows separation of concerns. What you have done is implemented INotifyPropertyChange on the actual UI class which has no purpose. The UI class has full access to its UI elements so why use extra eventing (i.e. PropertyChange)?


Secondly your setter logic is a bit odd. You are adding a value to a collection and then always setting the "return" value to be the first item. This means it will never change after an item is added.

And lastly you are not posting the property change on the setter. I see it in a button click but the button click does not do anything else.

Not sure why you have a collection but try something more simple:

private string firstno;
public string FirstNo
{
    get
    {
        return firstno;
    }
    set
    {
       if (_firstNo != value)
      {
            firstno = value;
            NotifyPropertyChange(string "FirstNo");
       }
    }
}



另外,如果您遵循我的建议,并且实际上将对象(ViewModel和View)分开,则需要考虑什么类型的绑定.这取决于您是从视图模型(OneWay->默认值),视图(OneWayToSource)还是从两者(TwoWay)接收数据.在某些情况下,您也可以使用OneTime,但这很少见.
也许回顾一下MSDN上的一些内容以提供帮助.

http://msdn.microsoft.com/en-us/library/ms752347.aspx [ ^ ]



Also, if you follow my recomendation and actually separate the objects (ViewModel and View) you want to consider what type of binding. This depends on if you are receiving the data from your view model (OneWay -> default), view (OneWayToSource), or both (TwoWay). Also there can be some cases where you use OneTime but that is rare.
Maybe review some of the stuff on MSDN to help.

http://msdn.microsoft.com/en-us/library/ms752347.aspx[^]


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

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