Rahul Saksule
Rahul Saksule

Reputation: 417

How to bind Background to the StackPanel

I am developing Windows8 store app.I have Grid which is populating dynamically

<Grid Grid.Column="1" Margin="0,16,0,0" HorizontalAlignment="Left" VerticalAlignment="Center">
                <GridView x:Name="chapterlist" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" ItemClick="onChapterClick" Padding="0" Height="600" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Width="260" Height="80" Background="{Binding RelativeSource={RelativeSource Self}, Path=alreadyDownload, Converter={StaticResource ColorConverter}}">                                    
                                <TextBlock x:Name ="AAA" Text="{Binding Path=Chapter}" FontSize="10" Foreground="White" d:LayoutOverrides="Width" Margin="5" TextWrapping="Wrap" />
                                <TextBlock Text="{Binding Path=Name}" Foreground="White" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
                                <TextBlock Text="{Binding Path=alreadyDownload}" Foreground="#073363" VerticalAlignment="Bottom"  HorizontalAlignment="Right" Margin="0,18,2,2" FontSize="10" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
                            </StackPanel>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                </GridView>
            </Grid>

So i have to change the background color of StackPanel according to TextBlock value like

<TextBlock Text="{Binding Path=alreadyDownload}" Foreground="#073363" VerticalAlignment="Bottom"  HorizontalAlignment="Right" Margin="0,18,2,2" FontSize="10" d:LayoutOverrides="Width" TextWrapping="Wrap"/>

I have used ColorConverter like

class ColorConverter : IValueConverter
{
    public object Convert(object value, System.Type targetType, object parameter, String culture)
    {
        if (value != null)
        {
            if (value.Equals("Already Downloaded "))
                return Colors.Red;
            else
                return Colors.White;
        }

        return Colors.White;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

But my grid is not reflecting background color(infact no color at all,its transparent).Why it is happening? How can I solve this problem? Please help.I am attaching image for reference. Thanks in advance enter image description here

So what I want is to show Grid with text Already Downloaded should be having some other color and rest of the Grids with different color.

Upvotes: 1

Views: 1983

Answers (3)

Krish
Krish

Reputation: 376

Use this code instead:

if (value.Equals("Already Downloaded "))
        return Brushes.Red;
    else
        return Brushes.White;

Or If you want to use colors class you can use in this way.

new SolidColorBrush(Colors.Red)

Hope this may be solve your issue.

Upvotes: 0

Rahul Saksule
Rahul Saksule

Reputation: 417

There are two ways to do the above mentioned scenerio

1)We can add Background property to the objects that populates ObservableCollection and using binding in xaml

<Grid Width="200" Background="{Binding Background}" />

This way we can choose every item color in the grid and change it dynamically just changing object property. Here Background property must be a string assigned with a valid color like

object.Background = "White"

2)Using Converter(as I used) to convert some existing property in your object to a color(refer my Converter class). We can also bind using some property like I used here

<TextBlock Text="{Binding Path=alreadyDownload}"

which'll look like

<StackPanel Width="260" Height="80" Background="{Binding RelativeSource={RelativeSource Self}, Path=alreadyDownload, Converter={StaticResource ColorConverter}}">                                    
                            <TextBlock x:Name ="AAA" Text="{Binding Path=Chapter}" FontSize="10" Foreground="White" d:LayoutOverrides="Width" Margin="5" TextWrapping="Wrap" />
                            <TextBlock Text="{Binding Path=Name}" Foreground="White" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
                            <TextBlock Text="{Binding Path=alreadyDownload}" Foreground="#073363" VerticalAlignment="Bottom"  HorizontalAlignment="Right" Margin="0,18,2,2" FontSize="10" d:LayoutOverrides="Width" TextWrapping="Wrap"/>
                        </StackPanel>

So why my code is not running? Its because the IValueConverter returns object which should be a string, so instead of using

if (value.Equals("Already Downloaded "))
            return Colors.Red;
        else
            return Colors.White;

use

 if (value.Equals("Already Downloaded "))
                return "#FF0000";
            else
                return "#FFFFFF";

So its running perfectly

Upvotes: 1

Lucas
Lucas

Reputation: 3502

if (value.Equals("Already Downloaded "))

Is it maybe the space between Downloaded and " ?

Upvotes: 0

Related Questions