使用Canvas进行RaisePropertyChanged

时间:2012-01-31 19:22:29

标签: silverlight windows-phone-7 canvas prism

我在WP7应用程序中使用Prism作为MVVM模式。在我的ViewModel中,我实现了两个属性:

private IconVO _selectedIcon;
public IconVO SelectedIcon {
   get {
       return _selectedIcon;
   }
   set {
       _selectedIcon = value;
       SelectedIconCanvas = _selectedIcon.Icon;
       RaisePropertyChanged(() => this.SelectedIcon);
   }
}

private Canvas _selectedIconCanvas;
public Canvas SelectedIconCanvas {
   get {
        return _selectedIconCanvas;
   }
   set {
       _selectedIcon = value;
       RaisePropertyChanged(() => this.SelectedIconCanvas);
   }
}

其中IconVO(它存储从某个XML文件加载的单个图标信息):

public class IconVO {
   public string Name { get; set; }
   public Canvas Icon { get; set; }
} 

SelectedIcon 目前从ObservableCollection<IconVO>中选择了IconVO(集合绑定到ListPicker)。

SelectedIconCanvas 是一个存储来自Canvas的{​​{1}}的属性。

当我执行此代码时,应用程序抛出 ArgumentException - &gt;此行上的参数不正确

SelectedIcon.Icon

这段代码出了什么问题?

谢谢,fl4izdn4g

编辑01-02-2012

根据您的要求,这是XAML:

RaisePropertyChanged(() => this.SelectedIconCanvas);

我尝试将ContentControl替换为:

<Border Grid.Row="1" Background="{Binding SelectedColor}" >
  <ContentControl Margin="40,20,300,20" Content="{Binding SelectedIconCanvas}">
     <ContentControl.ContentTemplate>
        <DataTemplate>
           <ContentPresenter />
        </DataTemplate>
     </ContentControl.ContentTemplate>
  </ContentControl>    
</Border>

但它没有帮助。

2 个答案:

答案 0 :(得分:0)

就这样说:

private IconVO _SelectedIcon;
public IconVO SelectedIcon 
{
    get { return _SelectedIcon; }
    set 
    {
       _SelectedIcon = value;
       SelectedIconCanvas = _SelectedIcon.Icon;
       RaisePropertyChanged("SelectedIcon");
   }
}

private Canvas _SelectedIconCanvas;
public Canvas SelectedIconCanvas 
{
   get { return _SelectedIconCanvas; }
   set 
   {
       _SelectedIconCanvas = value;
       RaisePropertyChanged("SelectedIconCanvas");
   }
}

抱歉,我更改了您的属性名称(我有C#偏好)

答案 1 :(得分:0)

您不应该在MVVM中的视图模型中提到UI元素。也许你想引用所选画布的DataContext / view模型?