Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当数据绑定为无路径时触发PropertyChanged事件_C#_.net_Wpf_Xaml_Windows Phone 8 - Fatal编程技术网

C# 当数据绑定为无路径时触发PropertyChanged事件

C# 当数据绑定为无路径时触发PropertyChanged事件,c#,.net,wpf,xaml,windows-phone-8,C#,.net,Wpf,Xaml,Windows Phone 8,我有一个绑定到ImageMetadata类的ObservableCollection的列表框。Listbox的项模板定义为 <Image Source="{Binding Converter={StaticResource ImageConverter}}" /> ImageMetadata是编写为的“模型”类 public object Convert(object value, Type targetType, object parameter, System.Globaliz

我有一个绑定到ImageMetadata类的ObservableCollection的列表框。Listbox的项模板定义为

<Image Source="{Binding Converter={StaticResource ImageConverter}}" />
ImageMetadata是编写为的“模型”类

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var metadata = (ImageMetadata)value;
        if (metadata.IsPublic)
        {
            //code to return the image from path
        }
        else
        {
            //return default image
        }
     }
class ImageMetadata : INotifyPropertyChanged
{
    public string ImagePath
    {
        ......
    }

    public bool IsPublic
    {
        ......
    }
}
更新映像时,我将触发PropertyChanged事件,如下所示

NotifyPropertyChanged("ImagePath");
这里的问题是:NotifyPropertyChanged事件将不起作用,因为我将更改的属性名称指定为“ImagePath”,并且绑定到“ImageMetadata”对象而不是“ImagePath”属性

不能使用

<Image Source="{Binding ImagePath, Converter={StaticResource ImageConverter}}" />

因为我还需要IsPublic属性来决定显示哪个图像

如何修改代码以正确触发PropertyChanged事件


编辑:我正在为Windows phone 8开发。

您可以使用带有多值转换器的多绑定:

<Image>
    <Image.Source>
        <MultiBinding Converter="{StaticResource ImageConverter}">
            <Binding Path="ImagePath"/>
            <Binding Path="IsPublic"/>
        </MultiBinding>
    </Image.Source>
</Image>

以前有一次。看看它是否有用。设置
源属性的
样式如何,在样式内部,您可以使用
DataTriggers
定义
ImagePath
更改时发生的情况。您也可以使用MVVM方式添加视图模型类(可能来自
ImageMetadata
模型类),它通过另一个属性提供图像。@克莱门斯,你能解释一下吗?你有没有看顶部Icecat的评论?您是否推荐类似的内容?您可以添加另一个属性,该属性以类似于转换器的方式返回图像。但是,您可能不希望在模型类中具有此属性,因此将其放在“中间”类中,即所谓的视图模型中。您可以在web上搜索MVVM以获取有关此模式的更多信息,此模式已成为WPF、Silverlight和Windows应用商店应用程序中的标准体系结构模式。这不起作用-它是Windows Phone-谢谢,您的答案是信息丰富的。但是多重绑定对我没有帮助,因为Windows phone开发不支持它。对不起,我应该在问题中写清楚。@Fadi那么你也许应该从你的问题中删除
WPF
标记。
public object Convert(
     object[] values, Type targetType, object parameter,CultureInfo culture)
{
    object result = null;

    if (values.Length == 2 && values[0] is string && values[1] is bool)
    {
        var imagePath = (string)values[0];
        var isPublic = (bool)values[1];
        ...
    }

    return result;
}