2

I am adding items to my ListView manually. I am quite new to UWP and xaml. This is my xaml and c# codes:

public sealed partial class CalendarFlyout : SettingsFlyout
{
    public CalendarFlyout()
    {
        this.InitializeComponent();
        Width = 450;
        for (int i = 0; i < GlobalVars.table.Count; i++)
        {
                calendarFlyout.Items.Add(new Items { Time = sSplit[0], Country = sSplit[1], Title = sSplit[2], Results = sSplit[3] + "|" + sSplit[4] + "|" + sSplit[5], FlagImage = imagePath, bull1 = images[0], bull2 = images[1], bull3 = images[2], Color = new SolidColorBrush(Colors.LimeGreen)});
        }
        //change background here
    }
}

public class Items
{
    public string Time { get; set; }
    public string Country { get; set; }
    public string Title { get; set; }
    public string Results {get; set; }
    public string FlagImage { get; set; }
    public string bull1 { get; set; }
    public string bull2 { get; set; }
    public string bull3 { get; set; }
    public Brush Color { get; set; }
}

xaml:

<ListView x:Name="calendarFlyout" BorderThickness="0" ItemsSource="{Binding}" Width="450"> 
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Name="bord1" BorderBrush="#FFCDCDCD" BorderThickness="0,0,0,1" Width="450" VerticalAlignment="Stretch" HorizontalAlignment="Left">
                <Grid HorizontalAlignment="Left" Width="450" Height="50" Background="{Binding Color}">
                    <TextBlock x:Name="timeText" Text="{Binding Time}" Margin="0,0"/>
                    <TextBlock Name="countryText" Text="{Binding Country}" Margin="65,0,0,0"/>
                    <TextBlock Name="newsText" Text="{Binding Title}" Margin="120,0,0,0"/>
                    <TextBlock Name="resultText" Text="{Binding Results}" Margin="120,30,0,0" FontWeight="Bold"/>
                    <Image Margin="0,15,440,0" Source="{Binding bull1}" Stretch="Uniform"/>
                    <Image Margin="20,15,420,0" Source="{Binding bull2}" Stretch="Uniform"/>
                    <Image Margin="40,15,400,0" Source="{Binding bull3}" Stretch="Uniform"/>
                    <Image Name="flag" Margin="65,20,355,10" Source="{Binding FlagImage}" Stretch="Uniform"/>

                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

I am trying to change the background color of specific item. How can I access that item? Is there a way to set the background color after that the item is created rather then when it is created?

John P.
  • 1,199
  • 2
  • 10
  • 33
  • See [C# Change A Button's Background Color](http://stackoverflow.com/questions/4991041/c-sharp-change-a-buttons-background-color) and [Set background color of WPF Textbox in C# code](http://stackoverflow.com/questions/979876/set-background-color-of-wpf-textbox-in-c-sharp-code). Do they help? – Sam Hobbs Feb 28 '17 at 22:12
  • Can you access the Items object where you want to change the Color property in code or do you really need to access the Grid? – DevAttendant Feb 28 '17 at 22:48
  • Use `Binding` which is the recommended approach. – AnjumSKhan Mar 01 '17 at 04:35

2 Answers2

2

Firstly, you could access an item of the ListView like this way:

Items targetItem= calendarFlyout.Items[2] as Items;

Then, if you want to change its background color by assigning the target Brush value to that item, you need make your model class implement the interface INotifyPropertyChanged, so the definition of your model class would be like this:

public class Items : INotifyPropertyChanged
{
    public string Time { get; set; }
    public string Country { get; set; }
    public string Title { get; set; }
    public string Results { get; set; }
    public string FlagImage { get; set; }
    public string bull1 { get; set; }
    public string bull2 { get; set; }
    public string bull3 { get; set; }

    private Brush _brush;
    public Brush Color
    {
        get { return _brush; }
        set
        {
            _brush = value;
            RaisePropertyChanged("Color");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Finally, you could change its background color:

Items targetItem = calendarFlyout.Items[2] as Items;
targetItem.Color = new SolidColorBrush(Colors.Red);

Further more, as a suggestion, I think you'd better have a deeper leaning about INotifyPropertyChanged and MVVM design pattern.

WPInfo
  • 1,066
  • 1
  • 8
  • 21
0

calendarFlyout.Items[2].BackColor = Color.Blue;

George
  • 29
  • 4