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内のこれら 2 つの前景色を動的に変更する必要がありますStackPanel。問題は、それらが私の C# コードからアクセスできないように見えることですStackPanel

だから基本的にこれは私がする必要があることです:

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

しかし、C# コードでさえ見つけることができませんnoteTitle...どうすれば修正できますか?

ありがとうございました!

4

2 に答える 2

2

そのような目的には値コンバーターを使用することをお勧めします。FileNameフォアグラウンド値は、バインド先のオブジェクトの状態に依存すると想定しているDateCreatedため、このオブジェクトを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 テンプレート = 新しい 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 に答える