use*_*541 11 c# silverlight-4.0
我有一个继承自TextBox控件的自定义控件.我想INotifyPropertyChanged在我的自定义控件中实现该接口.
public class CustomTextBox : TextBox, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是当我尝试引发PropertyChanged事件时,PropertyChanged事件处理程序始终为null.
有人可以帮帮我吗?
你有什么期望?PropertyChanged事件由UI代码使用,但在您写作的意义上并非如此.控件从不实现INPC(INotifyPropertyChanged的简称),它们绑定到已实现 INPC的对象.这样,某些UI属性(例如TextBox控件上的Text属性)绑定到此类上的属性.这是MVVM架构的基础.
例如,您将编写以下XAML代码:
<TextBlock x:Name="txtBox" Text="{Binding Title}" />
Run Code Online (Sandbox Code Playgroud)
然后以下列方式在代码中设置TextBlock(或其任何祖先,传播DataContext)的数据上下文:
txtBox.DataContext = new Movie {Title = "Titanic"};
Run Code Online (Sandbox Code Playgroud)
现在为班级本身:
public class Movie : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
private string _title;
public string Title
{
get { return _title; }
set
{
if (_title == value) return;
_title = value;
NotifyPropertyChanged("Title");
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,无论何时更改Title属性,无论是在代码中还是通过其他绑定,UI都将自动刷新.Google for Data Binding和MVVM.
像我在评论中说的那样做。它不像你那样工作。只需创建一个额外的类,并实现Notification Interface那里,这个类应用到DataContext的UserControl。它会像你需要的那样工作。
public partial class W3 : UserControl
{
WeatherViewModel model = new WeatherViewModel();
public W3()
{
InitializeComponent();
this.DataContext = model;
}
public void UpdateUI(List<WeatherDetails> data, bool isNextDays)
{
model.UpdateUI(data, isNextDays);
}
}
class WeatherViewModel : INotifyPropertyChanged
{
public void UpdateUI(List<WeatherDetails> data, bool isNextDays)
{
List<WeatherDetails> weatherInfo = data;
if (weatherInfo.Count != 0)
{
CurrentWeather = weatherInfo.First();
if (isNextDays)
Forecast = weatherInfo.Skip(1).ToList();
}
}
private List<WeatherDetails> _forecast = new List<WeatherDetails>();
public List<WeatherDetails> Forecast
{
get { return _forecast; }
set
{
_forecast = value;
OnPropertyChanged("Forecast");
}
}
private WeatherDetails _currentWeather = new WeatherDetails();
public WeatherDetails CurrentWeather
{
get { return _currentWeather; }
set
{
_currentWeather = value;
OnPropertyChanged("CurrentWeather");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14038 次 |
| 最近记录: |