Data binding 在WP7上更改列表框的颜色属性

Data binding 在WP7上更改列表框的颜色属性,data-binding,windows-phone-7,listbox,Data Binding,Windows Phone 7,Listbox,我从WP7开始,尝试更改TexBlock的颜色和其他属性。它包含在列表框中并绑定到集合。我正在寻找一种像“OnDataBound”这样的方法。该值必须根据绑定对象的不同而更改 <ListBox HorizontalAlignment="Left" Name="listBox1" ItemsSource="{Binding}" > <ListBox.ItemTemplate> <DataTe

我从WP7开始,尝试更改TexBlock的颜色和其他属性。它包含在列表框中并绑定到集合。我正在寻找一种像“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>

感谢您的帮助

在本例中,颜色存储在一个名为ListItems的类中,该类定义为:

public class ListItems
{
    public string name { get; set; }
    public string color { get; set; }
}
可观察到的ListItems类型集合添加到主页文件的代码隐藏中:

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;
    }
}
public部分类主页:PhoneApplicationPage
{
ObservableCollection items=新的ObservableCollection();
//建造师
公共主页()
{
初始化组件();
添加(新列表项(){name=“Item 1”,color=“Red”});
添加(新列表项(){name=“Item 2”,color=“Blue”});
添加(新列表项(){name=“Item 3”,color=“Green”});
添加(新列表项(){name=“Item 4”,color=“White”});
添加(新列表项(){name=“Item 5”,color=“Purple”});
DataContext=this;
listBox1.ItemsSource=项目;
}
}
}

和ListItem类型的项被添加到可观察集合中

然后,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>

完成的页面如下所示:


希望这有帮助。

在本例中,颜色如果存储在一个名为ListItems的类中,该类定义为:

public class ListItems
{
    public string name { get; set; }
    public string color { get; set; }
}
可观察到的ListItems类型集合添加到主页文件的代码隐藏中:

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;
    }
}
public部分类主页:PhoneApplicationPage
{
ObservableCollection items=新的ObservableCollection();
//建造师
公共主页()
{
初始化组件();
添加(新列表项(){name=“Item 1”,color=“Red”});
添加(新列表项(){name=“Item 2”,color=“Blue”});
添加(新列表项(){name=“Item 3”,color=“Green”});
添加(新列表项(){name=“Item 4”,color=“White”});
添加(新列表项(){name=“Item 5”,color=“Purple”});
DataContext=this;
listBox1.ItemsSource=项目;
}
}
}

和ListItem类型的项被添加到可观察集合中

然后,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>

完成的页面如下所示:


希望这能有所帮助。

此时,我正在使用一个转换器(继承IValueConverter)并返回一个SolidColorBrush,我将其绑定到TextBlock的Forecolor属性上。这是最好的方法吗?现在,我正在使用一个转换器(继承了IValueConverter)并返回一个SolidColorBrush,它绑定在TextBlock的Forecolor属性上。这是最好的方式吗?谢谢。这是一个很好的解决方案,但我无法更改绑定的实体。但是我喜欢这个方法,并且会尽可能地使用它。谢谢。这是一个很好的解决方案,但我无法更改绑定的实体。但我喜欢这种方法,并将尽可能使用它。