【问题标题】:Change colour of data bound Itemtemplate on selected更改所选数据绑定项目模板的颜色
【发布时间】:2013-09-27 09:56:37
【问题描述】:

我有一个小问题。

我将一个 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 模板中特定项目的颜色?

我尝试通过数据绑定本身来更改颜色,但我愿意接受更简单的方法。

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

【问题讨论】:

  • 你试过 ColourClass 中的 INotifyPropertyChanged 了吗?

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


【解决方案1】:

更新 1

INotifyPropertyChanged 也适用于您。我正在提供最简单的演示,这将对您有所帮助。我怀疑你如何在字符串属性Colour 的帮助下绑定Foreground,而不使用实现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

【讨论】:

    【解决方案2】:

    想出办法。感谢 Xyroid 的代码,帮了大忙

    在课堂上:

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

    在方法中:

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

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 2017-06-19
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      相关资源
      最近更新 更多