【发布时间】:2018-04-10 09:22:53
【问题描述】:
我有一个实现IDictionary<string, object> 的类FormItem,当我绑定FormItem 的Value 属性时,PropertyChanged 事件始终为空,但是当我从@ 中删除IDictionary 987654327@ 那么FormItem.PropertyChanged 事件不为空。我想知道我在实现IDictionary 时为什么它为空以及如何修复它。
例子
public class FormItem : INotifyPropertyChanged, IDictionary<string, object>
{
public int _value;
public int Value
{
get { return _value; }
set
{
_value= value;
OnPropertyChanged("Value");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public bool CanEdit
{
get { return true; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
throw new NotImplementedException();
}
public void Add(KeyValuePair<string, object> item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(KeyValuePair<string, object> item)
{
throw new NotImplementedException();
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(KeyValuePair<string, object> item)
{
throw new NotImplementedException();
}
public int Count { get; }
public bool IsReadOnly { get; }
public void Add(string key, object value)
{
throw new NotImplementedException();
}
public bool ContainsKey(string key)
{
throw new NotImplementedException();
}
public bool Remove(string key)
{
throw new NotImplementedException();
}
public bool TryGetValue(string key, out object value)
{
throw new NotImplementedException();
}
public object this[string key]
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public ICollection<string> Keys { get; }
public ICollection<object> Values { get; }
}
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBox x:Name="text" Text="{Binding FormItem.Val}"></TextBox>
<Button x:Name="button" Visibility="{Binding FormItem.CanEdit}" Content="Hello World" />
</Grid>
</Grid>
</Page>
MainPage.xaml.cs:
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.DataContext= new DataPageViewModel();
}
}
}
DataPageViewModel.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace App1
{
class DataPageViewModel : INotifyPropertyChanged
{
private FormItem _formItem;
public FormItem FormItem
{
get { return _formItem; }
set
{
_formItem = value;
OnPropertyChanged("FormItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public DataPageViewModel()
{
this.FormItem = new FormItem();
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
【问题讨论】:
标签: c# data-binding uwp uwp-xaml inotifypropertychanged