依赖属性'PropertyChangedCallback未被调用(Dependency Properties' PropertyChangedCallback not getting called)
如果拥有一个带有DependencyProperty的自己的用户控件和相应的回调方法,如下所示:
public partial class PieChart : UserControl { public static readonly DependencyProperty RatesProperty = DependencyProperty.Register("Rates", typeof(ObservableCollection<double>), typeof(PieChart), new PropertyMetadata(new ObservableCollection<double>() { 1.0, 1.0 }, new PropertyChangedCallback(OnRatesChanged))); [...] public static void OnRatesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((PieChart)d).drawChart(); }
使用此用户控件时,我将名为“Rates”的ObservableCollection绑定到RatesProperty。 费率和相关方法如下所示:
private ObservableCollection<double> rates; public ObservableCollection<double> Rates { get { return this.rates; } set { if (this.rates != value) { this.rates = value; OnPropertyChanged("Rates"); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
当我更改ObservableCollection
this.Rates = new ObservableCollection<double>() { 1.0, 2.0 }
(例如,使用this.Rates = new ObservableCollection<double>() { 1.0, 2.0 }
1.0,2.0this.Rates = new ObservableCollection<double>() { 1.0, 2.0 }
)时,将调用用户控件的OnRatesChanged()方法,如预期的那样。 但是当我执行以下操作时,它不会被调用:this.Rates[0] = (double)1; this.Rates[1] = (double)2; OnPropertyChanged("Rates");
我期望当我使用正确的属性名称引发PropertyChanged事件时,始终会调用用户控件中的相应回调。 那是错的吗? 我找到了这个主题: 总是获取依赖属性的PropertyChangedCallback - Silverlight覆盖了Silverlight,但我认为在WPF中也是如此。
所以后台的框架检查绑定属性(在我的示例中为“Rates”)是否发生了变化,只有当它发生变化时才调用相关的回调,对吗? 因此改变我的收藏元素没有任何效果,我总是要改变完整的收藏?
谢谢!
If have an own user control with a DependencyProperty and the corresponding callback method like below:
public partial class PieChart : UserControl { public static readonly DependencyProperty RatesProperty = DependencyProperty.Register("Rates", typeof(ObservableCollection<double>), typeof(PieChart), new PropertyMetadata(new ObservableCollection<double>() { 1.0, 1.0 }, new PropertyChangedCallback(OnRatesChanged))); [...] public static void OnRatesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((PieChart)d).drawChart(); }
When using this user control, I bind an ObservableCollection called "Rates" to the RatesProperty. Rates and the related methods look like this:
private ObservableCollection<double> rates; public ObservableCollection<double> Rates { get { return this.rates; } set { if (this.rates != value) { this.rates = value; OnPropertyChanged("Rates"); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
When I change the ObservableCollection Rates (e.g. with
this.Rates = new ObservableCollection<double>() { 1.0, 2.0 }
) the OnRatesChanged() method of the user-control is called like expected. But when I execute the following, it is not called:this.Rates[0] = (double)1; this.Rates[1] = (double)2; OnPropertyChanged("Rates");
I expected that when I raise the PropertyChanged event with the correct property name, the corresponding callback in the user control is always called. Is that wrong? I found this thread: Getting PropertyChangedCallback of a dependency property always - Silverlight which covers silverlight but I think then that the same is true in WPF.
So the Framework in the background checks if the bound property (in my example "Rates") changed and only if it changed, it calls the associated callback, correct? Thus changing the elements of my collection has no effect, I always have to change the complete collection?
Thank you!
原文:https://stackoverflow.com/questions/12745060
最满意答案
这是故意的。 HTTP主体是字节,而不是文本。 如果要返回JSON文本,请将JSON编码为UTF-8(默认编码,在标准中指定,并且是唯一应该使用的编码)。
这也不是你的代码唯一的问题:
u
前缀和单引号是Python语法,而不是JSON。 相反,这样做:agent.request('POST',"http://localhost:8088/order", Headers({'Content-Type': ['application/json']}), StringProducer(u"{"aaa": 1}".encode("utf-8"))
这应该有效。
This is intentional. HTTP bodies are bytes, not text. If you want to return text for JSON, encode the JSON as UTF-8 (the default encoding, specified in the standard, and the only one you should use).
That's also not the only problem with your code:
u
prefixes and single quotes are Python syntax, not JSON. Instead, do this:agent.request('POST',"http://localhost:8088/order", Headers({'Content-Type': ['application/json']}), StringProducer(u"{"aaa": 1}".encode("utf-8"))
That ought to work.
相关问答
更多-
>>> u"ÿ".encode('raw-unicode-escape') '\xff' r"\u%04X" % ord(u"ÿ") This did the trick for me. It returns a string object ('\\u00FF') which I can use to make a string compare. It fails for unicode characters above U+FFFF but this is not necessary in my ca ...
-
C ++ UNICODE和STL(C++ UNICODE and STL)[2023-11-22]
一般来说,你不应该混合这两种编码。 但是,异常消息只是开发人员感兴趣的东西(例如,在日志文件中),不应该向用户显示(但请注意Jim的重要注意事项)。 因此,如果您使用UNICODE作为整个面向用户的界面,并仍在开发人员消息的幕后使用std::exception等,那么您就安全起来了。 应该不需要在两者之间进行转换。 此外,在C ++中为UNICODE独立的字符串定义typedef是一个很好的技巧: typedef std::basic_stringtstring; ...并类似地定义tco ... -
通过Unicode范围迭代(Iterate Through Unicode Ranges)[2022-11-09]
遗憾的是,VB.NET不像C#那样处理字符。 char实际上只是一个表示字母的数字(称为字符代码 ),因此对于计算机而言,您实际上可以在循环中使用它们。 但是,要使它在VB.NET中工作,您必须首先将字符转换为整数才能在循环中使用它们,然后将每次迭代中的整数转换回 Char : For i As Integer = AscW("A"c) To AscW("Z"c) Dim c As Char = ChrW(i) Yield c Next 至于你的第二个例子,Unicode代码点以U+### ... -
写文件不写“文本”:它写“字节”。 lstrlenW返回wchar_t的数量。 为了给第三个参数指定“要写入的字节数”,你需要乘以sizeof(wchar_t) Writefile does not write "text": it write "Bytes". The lstrlenW returns the number of wchar_t. You shold multiply by sizeof(wchar_t) in order to give to the third parameter th ...
-
这是故意的。 HTTP主体是字节,而不是文本。 如果要返回JSON文本,请将JSON编码为UTF-8(默认编码,在标准中指定,并且是唯一应该使用的编码)。 这也不是你的代码唯一的问题: u前缀和单引号是Python语法,而不是JSON。 相反,这样做: agent.request('POST',"http://localhost:8088/order", Headers({'Content-Type': ['application/json']}), ...
-
Lucene支持Unicode吗?(Does Lucene Support Unicode?)[2022-07-10]
是。 它完全支持unicode。 但为了分析,您应明确指定适当的词干和正确的词组。 至于样本。 这里是我们上一个项目的副本 directory = new RAMDirectory(); analyzer = new StandardAnalyzer(version, new Hashtable()); var indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFie ... -
您的数据已编码,很可能是utf-8。 Utf-8使用多个字节来编码非ascii字符,例如áéíóú 。 迭代编码为utf-8的字符串会产生构成字符串的单个字节 ,而不是您期望的字符 。 >>> s = 'áéíóúabcdefgçë' # There are 14 characters in s, but it contains 21 bytes >>> len(s) 21 >>> s '\xc3\xa1\xc3\xa9\xc3\xad\xc3\xb3\xc3\xbaabcdefg\xc3\xa7\xc3\ ...
-
Unicode转换(Unicode conversion)[2023-10-31]
我最终成功将所有模块移植到DMD 2.59,逐一修复所有问题。 I ended up succeeding to port all the modules to DMD 2.59 by fixing one by one all issues raised. -
简单地说,你可以使用字节数组传递它们,然后在另一端将其解释为unicode数组,但如果它不执行unicode,则它不会执行unicode。 将unicode字符串传递给无法解释它的库是没有意义的。 如果您需要执行某些特定操作(例如在具有unicode路径的文件系统上使用加载命令,例如HFS +),则不要。 而是使用系统提供的文件API并将数据推送到不合作的库构造函数中。 如果你真的遇到这个unicode文件路径业务的问题,导致你不能很好地传递地址和比特流,那么一个简单的解决方案是创建你自己的函数: obj_ ...
-
你不能划分unicode类型。 转换为整数或浮点数然后除以: amount = int(p) * ((1 + (int(r) / 100))**int(q)) You can't divide unicode types. Convert to ints or floats and then divide: amount = int(p) * ((1 + (int(r) / 100))**int(q))