将自定义依赖项属性绑定到ListView宽度不更新

时间:2016-01-11 19:54:20

标签: wpf data-binding dependency-properties

我有一个名为IValueConverter的{​​{1}},其中有一个名为CategoryToGridViewConverter的DependencyProperty。我还从ListViewWidthProperty继承了我的转换器,以便它可以与DependencyProperties一起使用。以下是我的转换器的定义方式:

DependencyObject

非常简单。然后我在XAML(在ListView资源部分)中创建这个转换器,如下所示:

internal class CategoryToGridViewConverter : DependencyObject, IValueConverter
    {
        public static readonly DependencyProperty ListViewWidthProperty = DependencyProperty.Register("ListViewWidth", typeof (double),
                                                                                                      typeof (CategoryToGridViewConverter), new PropertyMetadata(0d, PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            Debug.WriteLine("ListViewWidth: " + dependencyPropertyChangedEventArgs.NewValue);
        }

        public double ListViewWidth {
            get { return (double)GetValue(ListViewWidthProperty); }
            set { SetValue(ListViewWidthProperty, value); }
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //Not relevant
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //Not relevant
        }
    }
}

但问题是<ListView Grid.Row="0" BorderThickness="0" ItemsSource="{Binding SelectedCategory.Entries}" x:Name="listView" SizeChanged="ListView_OnSizeChanged"> <ListView.Resources> <local:CategoryToGridViewConverter x:Key="CategoryToGridViewConverter" ListViewWidth="{Binding listView, Path=Width, Mode=OneWay}"/> </ListView.Resources> <ListView.View> <Binding Path="SelectedCategory" Converter="{StaticResource CategoryToGridViewConverter}"/> </ListView.View> </ListView> 永远不会更新。它始终保持为0.但有问题的ListView显然没有0宽度。

我做错了什么?

编辑:它还在输出屏幕上显示绑定错误:

  

System.Windows.Data错误:2:找不到管理FrameworkElement   或目标元素的FrameworkContentElement。   BindingExpression:路径=宽度;的DataItem = NULL;目标元素是   'CategoryToGridViewConverter'(HashCode = 33594544);目标属性是   'ListViewWidth'(输入'Double')

DependencyProperties是否仅适用于FrameworkElements?如果是这样,我的转换器将如何了解ListView的宽度?

1 个答案:

答案 0 :(得分:1)

我认为你可以使用多值转换器。

<local:CategoryToGridViewMultiConverter x:Key="CategoryToGridViewConverter" />

转换器:

public class CategoryToGridViewMultiConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var category = (Category)values[0]; // Assumption: you're using a class named Category
        var listViewWidth = (double)values[1];

        // same logic you had before
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

用法:

<ListView.View>
    <MultiBinding Converter="{StaticResource CategoryToGridViewConverter}">
        <Binding Path="SelectedCategory" />
        <Binding ElementName="listView" Path="Width" Mode="OneWay" />
    </MultiBinding>
</ListView.View>