Skip to main content

Unanswered Question How to dynamically change color of the textblock inside gridRSS Feed

(0)

avinashkr
avinashkr

Member

Member

12 points

13 Posts

How to dynamically change color of the textblock inside grid

                           
In designmode this much code is written
                       <Stackpanel>           
                                  

 <ListBox x:Name="lbMenu" Width="110" SelectionChanged="lbMenu_SelectionChanged" Foreground="#FF316468">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid x:Name="gridView" ShowGridLines="False" Background="{Binding Color}"   >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="90">
                                       
                                        </ColumnDefinition>
                                        <ColumnDefinition Width="12"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                  
                                   
                                    <TextBlock Text="{Binding Text}" Foreground="{Binding ForeColor}" FontWeight="Bold" FontFamily="Tahoma"  Grid.Column="0" Height="18" Width="120"  FontSize="10.8">
                                       
                                        </TextBlock>
         
                                    
                                    <Image Grid.Column="1" Source="{Binding ImageUrl}" HorizontalAlignment="Left" Height="7" Width="4">
                                    </Image>
                                   
                                </Grid>

                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>

 

but when i changing the background color and foreground in code behind it changes only once after that it becomes empty even no text is shown ? can anyone explain me what is the problem?

nosuic
nosuic

Member

Member

517 points

135 Posts

Re: How to dynamically change color of the textblock inside grid

I implemented your code but the ItemTemplate doesn't work, in particular Color and ForeColor.. weird indeed. I guess that all Items of a listbox must have the same style, and colors are parts of it.

avinashkr
avinashkr

Member

Member

12 points

13 Posts

Re: Re: How to dynamically change color of the textblock inside grid

 When I implemented it. It actually works once but not when i tried again to change. means if u click once then it will change but if click it twice or thrice it will become blank. I dont know why it happens.

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25068 points

2,749 Posts

Re: How to dynamically change color of the textblock inside grid

Hello, what's you data source? Is it a class that implements INotifyPropertyChanged? If not, when a property in the data source changes, the modification won't be displayed on the Controls. Try something like this:

public class Source : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

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

private Brush foreColor;

public Brush ForeColor
{
get { return foreColor; }
set
{
foreColor = value;
NotifyChange("ForeColor");
}
}

//Other fields and properties.
}

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

avinashkr
avinashkr

Member

Member

12 points

13 Posts

Re: How to dynamically change color of the textblock inside grid

 I already tried it but it wont work. it works once or twice only.

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25068 points

2,749 Posts

Re: Re: How to dynamically change color of the textblock inside grid

Can you post more code or even a sample project?

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
  • Unanswered Question
  • Answered Question
  • Announcement