Skip to main content
Microsoft Silverlight
Home Forums Silverlight Design Designing with Silverlight How to dynamically change color of the textblock inside grid
5 replies. Latest Post by Yi-Lun Luo - MSFT on September 15, 2008.
(0)
avinashkr
Member
12 points
13 Posts
09-11-2008 2:08 AM |
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
517 points
135 Posts
09-11-2008 10:25 AM |
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.
09-11-2008 11:38 PM |
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 L...
All-Star
25068 points
2,749 Posts
09-14-2008 11:24 PM |
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.}
09-15-2008 12:34 AM |
I already tried it but it wont work. it works once or twice only.
09-15-2008 12:36 AM |
Can you post more code or even a sample project?