[英]WPF C# Code not working
在这里,我将放置3个文件的C#WPF代码(每个MVVM模型1个)和查看模型代码文件(UI.xaml.cs)。 我无法将ListBox与“第一个文本”框绑定在一起。 需要你的帮助。 我想要的是,只要在文本框中输入文本,就应该将其添加到列表框中。
我是WPF和MVMM的初学者。 抱歉,如果代码不够专业。 我们总是欢迎您的建议。
Form.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace MVVM.Models
{
public class Form : INotifyPropertyChanged
{
public Form()
{
}
#region Variables
private String _Name;
public String Name
{
get { return _Name; }
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
private String _colorBack;
public String ColorBack
{
get { return _colorBack; }
set
{
_colorBack = value;
OnPropertyChanged("ColorBack");
}
}
private String _colorFont;
public String Color_Font
{
get { return _colorFont; }
set
{
_colorFont = value;
OnPropertyChanged("ColorFont");
}
}
#endregion
#region INotifyPropertyChangedMembers
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
FormViewModel.cs
using MVVM.Models;
using MVVM.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace MVVM.ViewModels
{
public class FormViewModel
{
private Form _Form;
private ObservableCollection<string> listItems = new ObservableCollection<string>();
public FormViewModel()
{
_Form = new Form();
}
public Form Form
{
get { return _Form; }
}
public ObservableCollection<string> notifyChangedList()
{
listItems.Add("XYZ");
return listItems;
}
}
}
UI.xaml
<Window x:Class="MVVM.Views.UI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:MVVM.ViewModels"
Title="UI" Height="300" Width="300">
<Grid>
<TextBox TextWrapping="Wrap" Text="{Binding Name}" Margin="20,15,160,225" />
<TextBox TextWrapping="Wrap" Text="Back Color" Margin="20,73,195,167" />
<TextBox TextWrapping="Wrap" Text="Font Color" Margin="20,122,195,118"/>
<ListBox Name="listBox" ItemsSource="{Binding listItems}" HorizontalAlignment="Left" Margin="156,42,0,143" Height="85" Width="93" />
<Button Content="Finish" HorizontalAlignment="Left" Margin="112,163,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
UI.xaml.cs
using MVVM.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MVVM.Views
{
public partial class UI : Window
{
public UI()
{
InitializeComponent();
DataContext = new MVVM.ViewModels.FormViewModel();
}
}
}
好吧,我帮您解决。
您的文本框绑定为Name,但在FormViewModel
中没有属性名称Name
。
<TextBox TextWrapping="Wrap" Text="{Binding Name}" Margin="20,15,160,225" />
因此,您需要在FormViewModel
类中添加此属性。
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
}
}
当您在文本框中输入内容(更改焦点)时,将调用名称的设置器。 因此,您可以在setter中添加listItems
集合。
set
{
_name = value;
listItems.Add(_name);
}
如果您确实希望在按下文本框时(在按键时)收到通知,则需要实现自定义依赖项属性,而不仅仅是一个属性。 这篇文章解释了如何。
PS:让我知道浏览器中是否有错,请注意错别字,代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.