在WP7上更改Listbox的颜色属性

时间:2011-08-07 17:37:17

标签: data-binding windows-phone-7 listbox

我从WP7开始,我正在尝试更改TexBlock的颜色和其他属性。它包含在Listbox中并绑定到集合。我正在寻找像'OnDataBound'这样的方式。该值必须根据绑定对象进行更改。

<ListBox HorizontalAlignment="Left"  Name="listBox1"  ItemsSource="{Binding}"  >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="{StaticResource PhoneForegroundBrush}" Width="418" BorderThickness="1" Margin="2">
                            <StackPanel Orientation="Vertical" >
                                <StackPanel Orientation="Horizontal"><TextBlock Text="Charater: "/><TextBlock Text="{Binding Path=CharacterName}" TextWrapping="Wrap" /></StackPanel>
                                <StackPanel Orientation="Horizontal"><TextBlock Text="Perk launched: "/><TextBlock Text="{Binding Path=CreationDate}" TextWrapping="Wrap"/></StackPanel>
                                <StackPanel Orientation="Horizontal"><TextBlock Text="Finished at: "/><TextBlock Text="{Binding Path=FinishedAt}" TextWrapping="Wrap"/></StackPanel>
                                <StackPanel Orientation="Horizontal"><TextBlock x:Name="TextBlockStatus" Text="Status: "/><TextBlock Text="{Binding Path=Status}" TextWrapping="Wrap"/></StackPanel>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

依赖于绑定对象的状态,我需要更改Textblock的颜色(颜色等) 你能告诉我这一点吗?

我的约束是这样的(以.cs为单位):

 this.listBox1.DataContext = this.calculatedValues;

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

在此示例中,颜色存储在名为ListItems的类中,该类定义为:

public class ListItems
{
    public string name { get; set; }
    public string color { get; set; }
}

类型ListItems的可观察集合被添加到MainPage文件的后面代码中:

public partial class MainPage : PhoneApplicationPage
{

    ObservableCollection<ListItems> items = new ObservableCollection<ListItems>();
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        items.Add(new ListItems() { name = "Item 1", color = "Red" });
        items.Add(new ListItems() { name = "Item 2", color = "Blue" });
        items.Add(new ListItems() { name = "Item 3", color = "Green" });
        items.Add(new ListItems() { name = "Item 4", color = "White" });
        items.Add(new ListItems() { name = "Item 5", color = "Purple" });

        DataContext = this;
        listBox1.ItemsSource = items;
    }
}

}

ListItem类型的项目被添加到observable集合中。

然后,MainPage.xaml文件被设计为包含一个列表框,其ItemTemplate绑定到ListItem类的属性:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox HorizontalAlignment="Left" Margin="12" Width="400" Height="400" Name="listBox1" VerticalAlignment="Top" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock x:Name="Item" Text="{Binding name}" FontFamily="Arial" FontSize="40" Foreground="{Binding color}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

完成的页面如下所示:

enter image description here

希望这有帮助。