0
<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>

我需要动态TextBlocks更改StackPanel. 问题是它们似乎无法从我的 C# 代码中访问,假设因为它们在StackPanel.

所以基本上这就是我需要做的:

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

但我什至无法noteTitle在我的 C# 代码中找到......我该如何解决这个问题?

谢谢!

4

2 回答 2

2

我建议为此目的使用价值转换器。我假设前景值取决于您绑定到的对象的状态FileNameDateCreated因此只需将此对象用作 converetr 参数并在转换器中进行主要计算以决定应为该特定项目返回哪个前景。

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}}" />
于 2013-02-04T11:41:47.667 回答
0

如果您想获取元素,可以使用以下代码: 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;
                }
        }

或者你想改变模板,你可以通过代码创建模板,并覆盖模板:

DataTemplate 模板 = 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;
于 2013-02-05T11:02:16.100 回答