我有一个数据模板的列表框,其中包含一些文本块和组合框。我想在textblock和combobox上应用一些动画,就像我想在双击时更改文本块颜色一样。 因此我尝试为此创建故事板颜色动画,但我收到了以下错误
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>
我想知道我是否以正确的方式前进,或者有更好的方法来实施 列表框的datatemplate内的textblock上的颜色动画? 很想得到所有可能的建议。提前谢谢。
修改 这是我用来启动故事板的代码;
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>
我想改变&#34; textblock1&#34;的背景颜色。
答案 0 :(得分:0)
好像您的问题是从您拥有的包含TextBlock
对象访问ListBoxItem
元素。这是一个相当常见的问题,将在MSDN上的How to: Find DataTemplate-Generated Elements页面中详细讨论。
但是,简而言之,您需要使用ItemContainerGenerator.ContainerFromItem
方法来访问已应用于DataTemplate
中对象的ListBoxItem
中定义的UI元素。以下是链接页面的简短示例:
// 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);