DarioDP
DarioDP

Reputation: 627

How can I access properties of an item in a StackPanel in C#?

<ListBox x:Name="noteListBox" 
                 VerticalAlignment="Stretch" 
                 HorizontalAlignment="Stretch" Foreground="#FF329BD6" Margin="0,24,0,85">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock x:Name="noteTitle"
                            FontSize="40"
                            Text="{Binding Titolo}"
                            Tag="{Binding FileName}"
                            Foreground="#FF45B1EE" Tap="apriNota" Margin="5,0,0,0" />
                        <TextBlock x:Name="noteDateCreated"
                            Text="{Binding DateCreated}"
                            Margin="10,0,0,10" Foreground="#FF60ADD8" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I need to dynamically change the Foreground color of those two TextBlocks inside the StackPanel. The problem is that they don't seem to be accessible from my C# code, suppose because they're in that StackPanel.

So basically this is what I need to do:

noteTitle.Foreground = new SolidColorBrush(Color.FromArgb(255, 50, 155, 214));

But I can't even find noteTitle in my C# code... How can I fix this?

Thank you!

Upvotes: 0

Views: 167

Answers (2)

Jasber
Jasber

Reputation: 1

if you want get element you can use this code: var a = noteListBox.ItemTemplate.LoadContent() as StackPanel; (a as StackPanel).Background = Brushes.Red;

        foreach (UIElement  child in a.Children)
        {
            if (child is TextBlock)
                if ((child as TextBlock).Name.CompareTo("noteDateCreated") == 0)
                {
                    (child as TextBlock).Foreground = Brushes.Red;
                }
        }

or you want change change Template ,you can create Template by code,and overWrite Template:

DataTemplate template = new DataTemplate(typeof(Test)); FrameworkElementFactory spOuterFactory =new FrameworkElementFactory(typeof(StackPanel)); FrameworkElementFactory block1 = new FrameworkElementFactory(typeof(TextBlock)); FrameworkElementFactory block2 = new FrameworkElementFactory(typeof(TextBlock));

        block1.SetValue(TextBlock.ForegroundProperty, Brushes.Red);
        Binding binding1 = new Binding();
        binding1.Path = new PropertyPath("Titolo");
        block1.SetBinding(TextBlock.TextProperty, binding1);

        block2.SetValue(TextBlock.ForegroundProperty, Brushes.Red);
        Binding binding2 = new Binding();
        binding2.Path = new PropertyPath("noteDateCreated");
        block2.SetBinding(TextBlock.TextProperty, binding2);         


        spOuterFactory.AppendChild(block1);
        spOuterFactory.AppendChild(block2);

        template.VisualTree = spOuterFactory;
        noteListBox.ItemTemplate = template;

Upvotes: 0

sll
sll

Reputation: 62544

I would suggest using value converter for such purposes. I assume foreground value depends on a state of object whcih ahs proeprties FileName, DateCreated you bind to, so just use this object as converetr parameter and in converter do main calculation to decide which Foreground should be returned for this particular item.

public class EntityToForegroundConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, 
                         Type targetType, 
                         object parameter, 
                         CultureInfo culture) 
    {
        var entity = value as IMyEntity;
        // TODO: determine a foreground value based on bound 
        // object proerrty values
    }

    public object ConvertBack(object value, 
                              Type targetType, 
                              object parameter, 
                              CultureInfo culture)
    {
        return null;
    }
}
<Control.Resources>
    <src:EntityToForegroundConverter x:Key="foregroundConverter"/>
</Control.Resources>

<TextBlock 
   Foreground="{Binding Converter={StaticResource foregroundConverter}}" />

Upvotes: 2

Related Questions