首页 \ 问答 \ 依赖属性'PropertyChangedCallback未被调用(Dependency Properties' PropertyChangedCallback not getting called)

依赖属性'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.0 this.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
更新时间:2022-06-02 07:06

最满意答案

这是故意的。 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 ...
  • 一般来说,你不应该混合这两种编码。 但是,异常消息只是开发人员感兴趣的东西(例如,在日志文件中),不应该向用户显示(但请注意Jim的重要注意事项)。 因此,如果您使用UNICODE作为整个面向用户的界面,并仍在开发人员消息的幕后使用std::exception等,那么您就安全起来了。 应该不需要在两者之间进行转换。 此外,在C ++中为UNICODE独立的字符串定义typedef是一个很好的技巧: typedef std::basic_string tstring; ...并类似地定义tco ...
  • 遗憾的是,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']}), ...
  • 是。 它完全支持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\ ...
  • 我最终成功将所有模块移植到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))

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)