更改选定的数据绑定Itemtemplate的颜色

时间:2013-09-27 09:56:37

标签: data-binding windows-runtime microsoft-metro windows-store-apps winrt-xaml

我有一个小问题。

我将XML文件绑定到itemGridView和itemListView

Databind :(工作得很好,只是提供我在这里所做的)

 var data = from query in xdoc.Descendants("Colour")
                       select new ColourClass
                       {
                           Colour = "FFFF0000"

                       };
            itemGridView.DataContext = data;
            itemListView.DataContext = data;

我想在选择网格中的项目时更改文本的颜色(永久更改颜色)。我写了这个:它似乎不起作用。

    void ItemView_ItemClick(object sender, ItemClickEventArgs e)
    {

        ((ColourClass) e.ClickedItem).Colour = "#FF46FF00";

    }

我的XAML:

<GridView
            x:Name="itemGridView"
            AutomationProperties.AutomationId="ItemsGridView"
            AutomationProperties.Name="Items"
            TabIndex="1"
            Grid.RowSpan="2"
            Padding="116,136,116,46"
            ItemsSource="{Binding}"
            ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
            SelectionMode="None"
            IsSwipeEnabled="false"
            IsItemClickEnabled="True"
            ItemClick="ItemView_ItemClick"/>

标准模板:

<DataTemplate x:Key="Standard250x250ItemTemplate">
    <Grid HorizontalAlignment="Left" Width="400" Height="60">
        <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
            <TextBlock Text="test" Foreground="{Binding Colour, Mode=TwoWay}" Style="{StaticResource AppIDTextStyle}" Height="60" Margin="15,0,15,0"/>
        </StackPanel>
    </Grid>
</DataTemplate>

如何更改gridview中使用的Standard 250模板中特定项目的颜色?

我试图通过数据绑定本身来改变颜色,但是我更容易做到这一点。

我需要做的就是当用户点击项目时,项目的颜色从红色变为绿色。

2 个答案:

答案 0 :(得分:0)

更新1

INotifyPropertyChanged也适合您。我正在提供最简单的演示,它将为您提供帮助。我怀疑如何在不使用实现Foreground的转换器类的情况下,在字符串属性Colour的帮助下绑定IValueConverter

XAML

<GridView x:Name="gv" SelectionMode="None" IsItemClickEnabled="True" ItemClick="gv_ItemClick_1">
    <GridView.ItemTemplate>
        <DataTemplate>
            <TextBlock FontSize="20" Text="{Binding Color}" Width="200" />
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

C#

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    gv.ItemsSource = new List<ColourClass> 
    {
        new ColourClass("abc"),
        new ColourClass("dsd"),
        new ColourClass("yhd"),
        new ColourClass("nve"),
        new ColourClass("a3e"),
    };
}

private void gv_ItemClick_1(object sender, ItemClickEventArgs e)
{
    ((ColourClass)e.ClickedItem).Color = "#FF46FF00";
}


public class ColourClass : INotifyPropertyChanged
{
    private string _Color;
    public string Color
    {
        get { return _Color; }
        set { _Color = value; OnPropertyChanged("Color"); }
    }

    public ColourClass(string c)
    {
        Color = c;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string Prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(Prop));
        }
    }
}

这些会对你有帮助。

Metro App ListView SelectedItem Selected VisualState

Controlling The DataTemplate

答案 1 :(得分:0)

弄清楚如何。感谢Xyroid的代码,帮了很多

在课堂上:

    private string _colour;

    public string Colour
    {
        get { return _colour; }
        set
        {
            _colour = value;
            NotifyPropertyChanged("Colour");
        }
    }

在方法中:

   ((AppToDownload) e.ClickedItem).Colour = "#FF46FF00";