Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why don't you do it the <strong>Fast</strong> way instead of crawling the Visual Tree.</p> <pre><code>&lt;TextBlock FontSize="35" Text="{Binding Title}" Width="386" Foreground="[Binding Color}" /&gt; </code></pre> <p>Then all you have to do is:</p> <ol> <li>Add a <code>Color</code> Brush Property in your Collection</li> <li>Change this property to the color you want</li> <li>Make sure this property implement INotify or is a Dependency Property</li> </ol> <h2>Example</h2> <h3>XAML</h3> <pre><code>&lt;Grid&gt; &lt;ListBox ItemsSource="{Binding}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Title}" Foreground="{Binding TitleColor}" /&gt; &lt;TextBlock Text="{Binding Date}" Foreground="Black" /&gt; &lt;TextBlock Text="{Binding Id}" Visibility="Collapsed" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; </code></pre> <h3>CodeBehind</h3> <pre><code>public partial class MainPage : Page { public ObservableCollection&lt;TEST&gt; TestCollection { get; private set; } public MainWindow() { InitializeComponent(); TestCollection = new ObservableCollection&lt;TEST&gt;(); TestCollection.Add(new TEST() { TitleColor = Brushes.Black, ID = 0, Title = "A", Date = DateTime.Now, }); TestCollection.Add(new TEST() { TitleColor = Brushes.Red, ID = 1, Title = "B", Date = DateTime.Now.AddDays(1), }); DataContext = TestCollection; } } public class TEST : INotifyPropertyChanged { private Brush _TitleColor; public Brush TitleColor { get { return _TitleColor; } set { _TitleColor = value; OnPropertyChanged("TitleColor"); } } private int _ID; public int ID { get { return _ID; } set { _ID = value; OnPropertyChanged("ID"); } } private string _Title; public string Title { get { return _Title; } set { _Title = value; OnPropertyChanged("Title"); } } private DateTime _Date; public DateTime Date { get { return _Date; } set { _Date = value; OnPropertyChanged("Date"); } } public TEST() { } #region INotifyProperty public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload