Xaml 具有从属特性的图像源转换器

Xaml 具有从属特性的图像源转换器,xaml,binding,windows-phone-8,converter,dependency-properties,Xaml,Binding,Windows Phone 8,Converter,Dependency Properties,我正在开发一个WP8MVVM应用程序,我遇到了一个绑定问题 我有这个XAML代码: <UserControl.Resources> <local:CurrentCategorySourceConverter x:Key="CurrentCategorySourceConverter"/> </UserControl.Resources> 问题是我无法在XAML中绑定转换器的isCurrent属性,因为在创建usercontrol时datacontext为nu

我正在开发一个WP8MVVM应用程序,我遇到了一个绑定问题

我有这个XAML代码:

<UserControl.Resources>
<local:CurrentCategorySourceConverter x:Key="CurrentCategorySourceConverter"/>
</UserControl.Resources>
问题是我无法在XAML中绑定转换器的isCurrent属性,因为在创建usercontrol时datacontext为null

以下代码导致XAML异常:

<UserControl.Resources>
<local:CurrentCategorySourceConverter x:Key="CurrentCategorySourceConverter"
                                      isCurrent="{Binding currentCategory}"/>
</UserControl.Resources>
问题是,当我从currentCategory上的模型引发属性更改时,转换器和图像源不会更改

这就是我试图用转换器实现的:当类别为当前值时,图像的源应为局部值,当类别为非当前值时,图像的源应为另一个值。我试图使用dependency属性并引发change事件,以便在类别当前状态更改时自动更改。我在互联网上搜索过,在我看来,它应该是有效的,但我肯定是做错了什么,因为它不起作用。非常感谢您的帮助

谢谢, 加泰林

编辑:

好的,我制作了一个绑定反射器,转换器参数现在是绑定的。但问题是它不是实时更新的


我有一个轴,每个项目中都有一个用户控件,其中包含很多图像。为了减少内存使用,我制作了这个转换器,使其仅显示当前透视项目的图像。但是当binded bool for category设置为true时,图像的源现在会更新,除非我重新加载页面。尽管对当前类别的两个属性、source和bool的更改会相互反映。

在调用构造函数内的InitializeComponenet之前,是否尝试设置DataContext?是的。但是,我的用户控件实际上位于一个pivot的pivot项中,该pivot项的itemsource是一个可观察的集合。所以datacontext就是从那里继承的。只有在加载用户控件后,我才能访问datacontext。多绑定转换器非常有用,但据我所知,它在WP8上不可用。我以另一种方式使其工作:
public class CurrentCategorySourceConverter : DependencyObject, IValueConverter
    {
        public bool isCurrent
        {
            get { return (bool)GetValue(CurrentCategoryProperty); }
            set { SetValue(CurrentCategoryProperty, value); }
        }

        public static DependencyProperty CurrentCategoryProperty =
            DependencyProperty.Register("isCurrent",
                                        typeof(bool),
                                        typeof(CurrentCategorySourceConverter),
                                        new PropertyMetadata(null));

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && isCurrent == true)
            {
                return value;
            }
            else
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<UserControl.Resources>
<local:CurrentCategorySourceConverter x:Key="CurrentCategorySourceConverter"
                                      isCurrent="{Binding currentCategory}"/>
</UserControl.Resources>
this.model = this.DataContext as CategoryPageContentViewModelApp;
converter = this.Resources["CurrentCategorySourceConverter"] as CurrentCategorySourceConverter;
Binding binding = new Binding();
binding.Source = this.model;
binding.Path = new PropertyPath("currentCategory");
BindingOperations.SetBinding(converter,CurrentCategorySourceConverter.CurrentCategoryProperty, binding);