我有一个ListBox
绑定到一个源,该源向内部控件的text属性提供数据。现在我想将我的文本框的Foreground
属性绑定到与主ListBox绑定的不同源之外的其他源!将ListBox内部的控件绑定到ListBox源以外的源
我的列表框绑定到的ObservableCollection,我想我的文字块前景财产势必文字颜色,位于视图模型
public SolidColorBrush textColor
{
get { return new SolidColorBrush(Colors.Red); }
}
都处于ViewModel
类。 我试过使用Foreground="{Binding textColor}"
,但似乎XAML根本看不到它,应该在页面中做任何事情,以便它可以看到它,还是因为父母(ListBox
)使用不同的来源?
编辑:
更多细节:
我有我在其中定义我的表一DataContext.cs
类。 我有一个ViewModel.cs
类,我在里面有这些
public class CViewModel : INotifyPropertyChanged
{
private CDataContext myDB;
public CViewModel(string DBConnectionString)
{
myDB = new CDataContext(DBConnectionString);
}
private ObservableCollection<Groups> _allGroups;
public ObservableCollection<Groups> AllGroups
{
get { return _allGroups; }
set
{
_allGroups = value;
NotifyPropertyChanged("AllGroups");
}
}
public string textColor
{
get { return "Tomato"; }
}
}
然后,我有我的XAML文件MainPage.xaml
:
....
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Margin="0,8,0,0" toolkit:TiltEffect.IsTiltEnabled="True" x:Name="list" ItemsSource="{Binding AllGroups}" HorizontalAlignment="Center" BorderThickness="4">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Orange" Width="125" Height="125" Margin="6" Tap="Counterlist_OnTap">
<TextBlock Name="gname" Foreground="White" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap"/>
<TextBlock Name="ccl" Margin="0,0,0,-5" Foreground="{Binding textColor}" Text="{Binding Count}" FontSize="26" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
....
我在代码中还设置我MainPage
至ViewModel
的DataContext
背后:
this.DataContext = App.ViewModel;
这应该很好地工作。我使用了一个默认的DataBound应用程序,将您的属性添加到ItemViewModel中,并在MainPage.xaml的第36行末尾添加了前景绑定,并在模拟器中运行时正确设置了文本块的颜色。请提供一个完整的示例来重现您的问题,我们将帮助您确定问题所在。 – 2013-03-04 17:54:49
谢谢我编辑了这篇文章! – user65248 2013-03-04 19:39:41
您现在已经定义了'textColor'两次。它是一个字符串还是一个SolidColorBrush? – 2013-03-04 21:43:03