依赖项属性绑定未更新 - c#

我定义了一个依赖属性,如下所示:

public static readonly DependencyProperty AnimateColumnWidthProperty =
    DependencyProperty.Register("AnimateColumnWidthProperty", typeof(double), typeof(MainWindow), new PropertyMetadata(0.0));

public double AnimateColumnWidth
{
    get { return (double)GetValue(AnimateColumnWidthProperty); }
    set { SetValue(AnimateColumnWidthProperty, value); }
}

当我的应用程序启动时,我将执行此操作。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    AnimateColumnWidth = Properties.Settings.Default.ProductInfoWidthExpanded;
}

...应将值初始化为其初始值-在这种情况下为400。

然后,我将UI中的网格列绑定到此属性...

<ColumnDefinition 
    Name="ProductInfo" 
    Width="{Binding Path=AnimateColumnWidth,
                    Converter={StaticResource doubleToGridLength},
                    Mode=TwoWay}" />

据我了解,由于列宽绑定到此属性,所以每当我更新该属性时,列宽也应更新。

更改属性时宽度没有更新,这是我做错了什么?我也在尝试通过动画也无法更新它。此外,永远不会命中在AnimateColumnWidth属性的getter上设置的断点-这意味着从未尝试过检索该属性。

(这确实很有效,我在某个地方坏了!!)

脚注:

转换后的值是在我的应用程序的根名称空间中定义的(我相信WPF如果找不到它会抱怨)。

[ValueConversion(typeof(Double), typeof(GridLength))]
public class DoubleToGridLength : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new GridLength((double)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((GridLength)value).Value;
    }
}

参考方案

您将属性注册为"AnimateColumnWidthProperty",这是“错误的”,只有字段名称是任意的,您可能希望使用"AnimateColumnWidth"(或者您当然更改了绑定,但是按原样,它失败了,因为路径指向未注册的属性)。

您可能还想阅读有关调试bindings的内容,然后可以发现此类错误,因为绑定引擎将报告这些错误。 (类似于“在对象y上找不到属性x”)。

同样在getter或setter中使用断点不会告诉您任何内容,绑定引擎不会使用它们,它们只是为了您自己的方便。

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

java.net.URI.create异常 - java

java.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…

Mongo汇总 - javascript

我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …

ImageButton中的无效回发或回调参数 - c#

我的Click事件删除按钮(图像按钮)时遇到问题。我知道对此还有其他疑问,但是我什么都不懂!我不明白没有答案!我做了所有其他所有问题中指定的内容!此页面中使“重定向”到示例的其他事件效果很好!我很清楚?需要更多细节吗?堆栈跟踪或更多代码?错误是: 无效的回发或回调参数。使用配置或页面中的启用事件验证。为了安全起见,此功能验证回发或回调事件的参数源自最初呈现它…

在ASP.NET WebForms中在服务器端初始化bootsrap datatimepicker - javascript

我有这个HTML<div class='datepicker input-group date' id='datetimepickerStart'> <input type='text' class="form-control" /> <span c…