如何从Windows Phone上的代码更改DataTemplate中TextBlock的前景色?

Shi*_*ada 1 windows-phone-7

我想从代码中更改DataTemplate中TextBlock的前景颜色(下面的TitleText和DateText).

<ListBox x:Name="listBox1" ItemsSource="{Binding}" ScrollViewer.ManipulationMode="Control" SelectionChanged="listBox1_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="stackPanel1" HorizontalAlignment="Stretch" Orientation="Horizontal">
                <TextBlock FontSize="35" x:Name="TitleText" Text="{Binding Title}" Width="386" Foreground="Black" />
                <TextBlock FontSize="25" x:Name="DateText" Text="{Binding Date}" Width="78" Foreground="Black" />
                <TextBlock x:Name="Id" Text="{Binding Id}" Visibility="Collapsed" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

我想在代码背后这样做.但它似乎无法访问DataTemplate中的x:Name属性.

this.TitleText.Foreground = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

有谁知道这个很好的解决方案?

MyK*_*SKI 6

为什么不用快速方式而不是爬行Visual Tree.

<TextBlock FontSize="35" Text="{Binding Title}" Width="386" Foreground="[Binding Color}" />
Run Code Online (Sandbox Code Playgroud)

那么你所要做的就是:

  1. Color在集合中添加画笔属性
  2. 将此属性更改为您想要的颜色
  3. 确保此属性实现INotify或是依赖属性

XAML

<Grid>
    <ListBox ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
                    <TextBlock Text="{Binding Title}" Foreground="{Binding TitleColor}" />
                    <TextBlock Text="{Binding Date}" Foreground="Black" />
                    <TextBlock Text="{Binding Id}" Visibility="Collapsed" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)

代码隐藏

public partial class MainPage : Page
{
    public ObservableCollection<TEST> TestCollection { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        TestCollection = new ObservableCollection<TEST>();
        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
}
Run Code Online (Sandbox Code Playgroud)