我有一個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