C# 如何设置ListBoxItem中定义的UI元素属性的动画?

C# 如何设置ListBoxItem中定义的UI元素属性的动画?,c#,wpf,xaml,C#,Wpf,Xaml,我有一个数据模板的列表框,它由一些文本块和组合框组成。我想在textblock和combobox上应用一些动画,就像我想在双击时更改textblock的颜色一样。 因此,我尝试创建故事板颜色的动画,但我得到以下错误 Cannot resolve all property references in the property path 'Color'. Verify that applicable objects support the properties. 我的故事板动画代码如下所示: &

我有一个数据模板的列表框,它由一些文本块和组合框组成。我想在textblock和combobox上应用一些动画,就像我想在双击时更改textblock的颜色一样。 因此,我尝试创建故事板颜色的动画,但我得到以下错误

Cannot resolve all property references in the property path 'Color'.
Verify that applicable objects support the properties.
我的故事板动画代码如下所示:

 <Storyboard x:Key="onSubmitAnimation">              
    <ColorAnimation From="Green" To="Red" Duration="0:0:5" 
                    Storyboard.TargetProperty="Color" />        
 </Storyboard>
我想我应该在setTarget函数中传递textblock的对象,但我不知道如何在listboxitem中获得正确的textblock对象

我的listbox名为Entrylistbox,因此我可以通过它访问listbox的任何项目,但不确定如何访问textblock并在其上应用动画

编辑2: 我仍然不能在textblock上应用动画,我得到了以下错误

 The method or operation is not implemented.
这是我的数据模板代码

<DataTemplate x:Key="DefaultDataTemplate" >
  <Canvas Height="62"  Width="600" Background="White" >
    <Image Source="{Binding Path=IconBinding, Converter={StaticResource imageConverter} }" 
           Canvas.Left="100" Canvas.Top="10" Height="35"/>

      <TextBlock Name="textblock1"  Padding="5" Canvas.Left="20" Canvas.Top="10" 
                 Background="LightGray" VerticalAlignment="Center" Height="35" 
                 Foreground="Gray" TextAlignment="Center" FontSize="16" 
                 FontFamily="/TimeSheet;component/Resources/#Open Sans Extrabold"  
                 Width="60" FontWeight="Bold">
      <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}H">
          <Binding Path="HoursBinding" />
        </MultiBinding>
      </TextBlock.Text>
    </Canvas>
  </DataTemplate>


我想更改“textblock1”的背景色。

似乎您的问题是从包含的
ListBoxItem
对象访问
TextBlock
元素。这是一个相当常见的问题,在MSDN页面中有详细讨论

但是,简而言之,您需要使用
ItemContainerGenerator.ContainerFromItem
方法来访问
DataTemplate
中定义的UI元素,该元素已应用于
ListBoxItem
中的对象。以下是链接页面中的一个简短示例:

// Getting the currently selected ListBoxItem 
// Note that the ListBox must have 
// IsSynchronizedWithCurrentItem set to True for this to work
ListBoxItem myListBoxItem =
    (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));

// Getting the ContentPresenter of myListBoxItem
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

// Finding textBlock from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);

// Do something to the DataTemplate-generated TextBlock
MessageBox.Show("The text of the TextBlock of the selected list item: "
    + myTextBlock.Text);
//获取当前选定的ListBoxItem
//请注意,列表框必须具有
//IsSynchronizedWithCurrentItem设置为True以使其工作
ListBoxItem myListBoxItem=
(ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
//获取myListBoxItem的ContentPresenter
ContentPresenter myContentPresenter=FindVisualChild(myListBoxItem);
//从在该ContentPresenter上设置的DataTemplate中查找textBlock
DataTemplate myDataTemplate=myContentPresenter.ContentTemplate;
TextBlock myTextBlock=(TextBlock)myDataTemplate.FindName(“TextBlock”,myContentPresenter);
//对DataTemplate生成的TextBlock执行某些操作
MessageBox.Show(“所选列表项的文本块的文本:”
+myTextBlock.Text);

TexBlock
没有
Color
属性。它有
背景
前景
画笔。您是否尝试过
故事板.TargetProperty=“front.Color”
故事板.TargetProperty=“Background.Color”
?我尝试过这些属性,虽然它没有给出任何错误,但它没有更改文本块的颜色。如何启动
故事板
?目标是什么?你需要发布更多相关信息code@dkozl我已经编辑了这个问题。谢谢你的帮助Hey@Sheridan谢谢你的回答,我试过你的答案,但还是有些问题。我已经添加了有问题的datatemplate代码。如果您没有解释错误的抛出位置,说明您的错误说明了(您的问题中的)某些内容,则没有多大帮助。另外,我也不知道如何比微软的页面更好地解释这种情况。我在“获取myListBoxItem的ContentPresenter”步骤中遇到了这个错误。在这一行中,“ContentPresenter myContentPresenter=FindVisualChild(myListBoxItem);”哇,真的吗???尝试查看链接页面上的下一个代码示例,您将在其中找到
FindVisualChild
方法的实现。快点。。。这是基本的东西。。。你当然可以自己做这件事?是的!明白了,我明天就试试。谢谢你的帮助
// Getting the currently selected ListBoxItem 
// Note that the ListBox must have 
// IsSynchronizedWithCurrentItem set to True for this to work
ListBoxItem myListBoxItem =
    (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));

// Getting the ContentPresenter of myListBoxItem
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

// Finding textBlock from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);

// Do something to the DataTemplate-generated TextBlock
MessageBox.Show("The text of the TextBlock of the selected list item: "
    + myTextBlock.Text);