【问题标题】:How to animate a property of a UI element defined inside a ListBoxItem?如何为 ListBoxItem 内定义的 UI 元素的属性设置动画?
【发布时间】:2014-04-25 16:39:12
【问题描述】:

我有一个包含一些文本块和组合框的数据模板列表框。我想在文本块和组合框上应用一些动画,就像我想在双击时更改文本块颜色一样。 因此,我尝试为此创建故事板彩色动画,但出现以下错误

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>

我想知道我的方法是否正确,或者有更好的方法来实现 列表框数据模板内的文本块上的彩色动画? 很想得到所有可能的建议。提前致谢。

编辑: 这是我用来启动情节提要的代码;

 ListBoxItem item = (ListBoxItem)sender;
 Storyboard sb = this.FindResource("onSubmitAnimation") as Storyboard;
 Storyboard.SetTarget(sb, item);
 sb.Begin();

我想我应该在 setTarget 函数中传递 textblock 的对象,但我不知道如何在 listboxitem 中获取正确的 textblock 对象。

我的列表框被命名为 Entrylistbox,因此我可以通过它访问列表框的任何项目,但不确定如何访问文本块并在其上应用动画。

编辑 2: 我仍然无法在文本块上应用动画,我收到以下错误

 The method or operation is not implemented.

这是我的 DataTemplate 代码

<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”的背景颜色。

【问题讨论】:

  • TexBlock 没有 Color 属性。它有BackgroundForeground 画笔。你试过Storyboard.TargetProperty="Foreground.Color"Storyboard.TargetProperty="Background.Color"吗?
  • 我尝试了这些属性,虽然它没有给出任何错误但它没有改变 textblock 的颜色。
  • 你如何开始你的Storyboard?目标是什么?您需要发布更多相关代码
  • @dkozl 我已经编辑了这个问题。感谢您的帮助

标签: c# wpf xaml


【解决方案1】:

您的问题似乎是从您拥有的包含 ListBoxItem 对象中访问 TextBlock 元素。这是一个相当普遍的问题,在 MSDN 上的How to: Find DataTemplate-Generated Elements 页面中进行了详细讨论。

但是,简而言之,您需要使用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);

【讨论】:

  • 嘿@Sheridan 感谢您的回答! ,我试过你的答案,但我仍然有一些问题。我已添加有问题的 datatemplate 代码。
  • 如果您没有解释错误是在哪里引发的,那么说明您的错误说明了某些内容(来自您的问题)的帮助不大。另外,我不确定如何比微软页面更好地解释这种情况。
  • 我在“获取 myListBoxItem 的 ContentPresenter”步骤中遇到此错误。在这一行 'ContentPresenter myContentPresenter = FindVisualChild(myListBoxItem);'
  • 哇,真的吗???尝试查看链接页面上的下一个代码示例,您将在其中找到 FindVisualChild 方法的实现。来吧......这是基本的东西......你肯定可以自己做这个吗?
  • 是的!知道了,明天试试感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多