绑定图像源到属性不起作用

时间:2014-05-01 20:03:34

标签: c# wpf

我在这个viewmodel中有其他绑定正常工作。我不知道为什么这个不是。我尝试了许多不同的方法来更新图像源绑定。一切都失败了。这是我见过的最常见的方式,这就是我在Windows Phone上的方式。我不确定为什么这不适用于WPF。

XAML

<ComboBox 
  ItemsSource="{Binding Keywords}" 
  SelectedItem="{Binding SelectedKeyword, Mode=TwoWay}">
  <ComboBox.ItemTemplate>
      <DataTemplate>
           <Label Width="280" Content="{Binding KeywordName}"/>   
      </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

<Grid>
 <Image Source="{Binding LinkedImage}"/>
</Grid>

viewmodel

public KeywordObject SelectedKeyword
{
    get { return _SelectedKeyword; }
    set { _SelectedKeyword = value; OnPropertyChanged("LinkedImage"); }
}

public Boolean _IsLinked
{
    get { return _SelectedKeyword.KeywordNumber.Length > 0; }
}

public ImageSource LinkedImage
{
    get 
    { 
        return _IsLinked ?

        new BitmapImage(new Uri("/images/checked.png", UriKind.RelativeOrAbsolute)) 
        :  
        new BitmapImage(new Uri("/images/unchecked.png", UriKind.RelativeOrAbsolute)); 
    }
}

public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string prop)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

这是在debuger中输出的例外

A first chance exception of type 'System.IO.IOException' occurred in PresentationFramework.dll
A first chance exception of type 'System.IO.IOException' occurred in PresentationCore.dll

1 个答案:

答案 0 :(得分:0)

答案最终是路径需要pack://application:,,,/Specific.Project.Name;component

以下是回答它的SO文章的链接

Image shows up in Visual Studio designer but not during run-time

这是最终结果的样子

<image Source="pack://application:,,,/Specific.Project.Name;component/images/checked.png"/>

我也可以将它注入绑定中,并且工作正常。

相关问题