2013-03-07 159 views
0

我检查过StackOverflow寻找答案,并且询问这里是我的最后一招,所以请不要downvote。我需要更有经验的人在数据绑定比我的意见: 这是我要绑定什么:BindingExpression路径错误 - 未在'MyObjectsVM'上找到'Color'属性

<Grid x:Name="all" Grid.Row="1"> 
    <TextBlock Text="all" Foreground="{Binding Color}" x:Name="allTxt" Grid.Row="0" Padding="10,21,0,0" FontWeight="Light" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Tap="TextBlock_Tap_1" /> 
</Grid> 

这就是我如何设置网格部件的DataContext的

public Multiplication() 
{ 
    InitializeComponent(); 
    this.all.DataContext = App.MyObjectsViewModel.allObjectsVisible; 
} 

这里是构造函数MyObjectsViewModel

public MyObjectsViewModel(DataContextManager manager) 
{ 
    allObjectsVisible = new allVisibleBtn() { Allvisible = true, Color = "white" }; 
} 

这里是allObjectsVisible属性:

private allVisibleBtn _allObjectsVisible; 
public allVisibleBtn allObjectsVisible 
{ 
    get { return _allObjectsVisible;} 
    set { _allObjectsVisible= value; NotifyPropertyChanged("allObjectsVisible"); } 
} 

最后这里是allObjectsBtn类:

public class allVisibleBtn : BaseViewModel 
    { 
     private bool _Allvisible; 
     public bool Allvisible 
     { get { return _Allvisible; } 
      set { NotifyPropertyChanging("Allvisible"); _Allvisible = value; NotifyPropertyChanged("Allvisible"); } 
     } 

     private string _Color; 
     public string Color 
     { 
      get { return _Color; } 
      set { if (value != null) { NotifyPropertyChanging("Color"); _Color = value; } NotifyPropertyChanged("Color"); } 
     } 
    } 

预期的行为正在改变文本块前景此刻有人轻拍它。 OFC还有一些更多的事情是在点击后完成的,但从问题的角度来看并不重要。 所以,请帮助我,为什么我得到以下错误:

`System.Windows.Data Error: BindingExpression path error: 'Color' property not found on 'Project.MyObjectsViewModel' 'Project.MyObjectsViewModel' (HashCode=50930930). BindingExpression: Path='Color' DataItem='Project.MyObjectsViewModel' (HashCode=50930930); target element is 'System.Windows.Controls.TextBlock' (Name='allTxt'); target property is 'Foreground' (type 'System.Windows.Media.Brush')..` 

回答

0

TextBlock的DataContext的似乎是Project.MyObjectsViewModel一个实例,以及该类型不具有颜色属性。因此,您需要将TextBlock的DataContext设置为allVisibleBtn的实例。尝试

<TextBlock DataContext="{Binding Path=allObjectsVisible}" ... /> 

一旦你得到那个工作,你需要改变allVisibleBtn的颜色属性的类型。 TextBlock的Foreground属性需要一个Brush,而不是一个字符串。