WPF TextBox绑定ElementName null [英] WPF TextBox Binding ElementName null

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

问题描述

我有一个带有TypeUsers的ListBox控件。当我在Listbox中选择一些记录并更新TextBox中的Name时,Name属性/ textbox将返回null。永远不要从TextBox获取价值,总是空的?



这是我的代码

I have a ListBox control with TypeUsers.When I select some record in Listbox and update Name in TextBox the Name property/textbox return always null. Never take value from TextBox, always null ?

This is my code

<ListBox x:Name="LstTypeUsers"

             Grid.Row="0" Grid.Column="4"

             Width="220" Height="120"

             ScrollViewer.VerticalScrollBarVisibility="Visible"

             ItemsSource="{Binding TypeUsers}"

             DisplayMemberPath="Name">
</ListBox>

<TextBox 

             Grid.Row="0" Grid.Column="2"

             x:Name="txtName"

             HorizontalAlignment="Left" Height="23" 

             TextWrapping="Wrap" 

             VerticalAlignment="Top" Width="170" 

             Text="{Binding ElementName=LstTypeUsers, Path=SelectedItem.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"

             Validation.ErrorTemplate="{x:Null}"/>

<Button 

            Grid.Column="0"

            HorizontalAlignment="Left" 

            VerticalAlignment="Top" 

            Width="100" Height="30"

            Command="{Binding UpdateTypeUserCmd}" 

            Grid.ColumnSpan="3" Margin="20,90,0,0">
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/Save.png" />
                <TextBlock Width="55" Height="18" ><Run Text="   "/><Run Text="Update"/></TextBlock>
            </StackPanel>
        </Button>







// Model class
public class UserType: INotifyPropertyChanged
{
    [Key]
    private int usertypeId;
    public int UserTypeId 
    { 
     get 
     {
         return this.usertypeId;
     }
     set
     {
         this.usertypeId = value;
         OnPropertyChanged("UserTypeId");
     }
    }

    [MaxLength(200)]
    private string name;
    public string Name
    { 
     get 
     {
         return this.name;
     }
     set
     {
         this.name = value;
         OnPropertyChanged("Name");
     }
    }

    [Required]
    private bool status;
    public bool Status
    { 
     get 
     {
         return this.status;
     }
     set
     {
         this.status = value;
         OnPropertyChanged("Status");
     }
    } 

    public virtual ObservableCollection<User> User { get;   private set; }

    public UserType()
    {
        this.User = new ObservableCollection<User>();

    }
}

// ViewModelBase class
 public event PropertyChangedEventHandler PropertyChanged;
 public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

// UserTypeViewModel

public class UserTypeViewModel: ViewModelBase

private UserType _userType;
private ObservableCollection<UserType> _UserTypeList;

// Constructor
public UserTypeViewModel()
{
   _userType = new UserType();
   _UserTypeList = new ObservableCollection<UserType>(GetUserTypeAll());
}

public ObservableCollection<TypeUsers> TypeUsers
{
  get
  {
     return _UserTypeList;
  }
  set
  {
     _UserTypeList = value;
     OnPropertyChanged("TypeUsers");
  }
}

public string Name
{
  get
  {
     return _userType.Name;
  }
  set
  {
      _userType.Name = value;
      OnPropertyChanged("Name");
  }
}







public ICommand UpdateTypeUserCmd
        {
            get { return new RelayCommand(k => UpdateTypeUser(Name, Status)); }
        }

        private void UpdateTypeUser(string name, bool status)
        {
            if (!IsStringMissing(name))
            {
                using (var typeUserManager = new TipUserManager())
                {
                    var typeUser = new TypeUser
                    {
                        TypeUserId = TypeUserId,
                        Name = name,
                        Status = status,
                    };

                    if (typeUserManager.Update(typeUser))
                    {
                        MessageBox.Show("Sucessfully!", "OK", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                    }
                }
            }
            else
                MessageBox.Show(Required fields!", "Warning!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

推荐答案

我已经解决了我的问题我已经在Model类中实现了INotifyPropertyChanged接口。我是WPF MVVM的新手,我读到这个接口只在ViewModel中实现,用于连接View。现在我已经在两个类中实现了,一切都很好。



谢谢。
I have resolved my problem. I have also implemented INotifyPropertyChanged interface in Model class. I am new in WPF MVVM and I read that this interface is implemented only in the ViewModel for connection with View. Now I have implemented in both classes and everything works great.

Thanks.


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

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