简体   繁体   English

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

[英]How to animate a property of a UI element defined inside a ListBoxItem?

I have a listbox of a data template which consist some textblock and combobox. 我有一个包含一些文本块和组合框的数据模板的列表框。 I want to apply some animations on textblock and combobox like i want to change textblock color on double click. 我想对文本块和组合框应用一些动画,就像我想双击更改文本块的颜色一样。 So for I tried creating storyboard color animation for that but I got following error 所以我尝试为此创建情节提要彩色动画,但出现以下错误

Cannot resolve all property references in the property path 'Color'.
Verify that applicable objects support the properties.

my storyboard animation code is something like this : 我的情节提要动画代码是这样的:

 <Storyboard x:Key="onSubmitAnimation">              
    <ColorAnimation From="Green" To="Red" Duration="0:0:5" 
                    Storyboard.TargetProperty="Color" />        
 </Storyboard>

I wonder whether I'm going in right way or not, or there is something better way to implement color animation on textblock inside datatemplate of listbox? 我想知道我是否走对了,还是有更好的方法在列表框数据模板内的文本块上实现彩色动画? Would love to get all possible suggestions. 希望得到所有可能的建议。 Thanks in advance. 提前致谢。

Edit: Here is the code I'm using to start storyboard; 编辑:这是我用来启动情节提要的代码;

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

I guess I should pass object of textblock in setTarget function but i dont know how to get correct object of textblock inside listboxitem. 我猜我应该在setTarget函数中传递textblock的对象,但我不知道如何在listboxitem中获取正确的textblock的对象。

My listbox is named as Entrylistbox so I can access any item of listbox through it but not sure how can i access textblock and apply animation on that. 我的列表框被命名为Entrylistbox,因此我可以通过它访问列表框的任何项目,但不确定如何访问文本块并在其上应用动画。

Edit 2: Still I'm not able to apply animation on textblock, I'm getting following error 编辑2:仍然无法在文本块上应用动画,但出现以下错误

 The method or operation is not implemented.

Here is my DataTemplate code 这是我的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>

I want to change background color of "textblock1" . 我想更改“ textblock1”的背景颜色。

It seems as though your problem is accessing the TextBlock element from the containing ListBoxItem object that you have. 似乎您的问题是从包含的ListBoxItem对象访问TextBlock元素。 This is a fairly common problem and is discussed in detail in the How to: Find DataTemplate-Generated Elements page on MSDN. 这是一个相当普遍的问题,在MSDN上的“ 如何:查找由DataTemplate生成的元素”页面中进行了详细讨论。

However, in short, you need to use the ItemContainerGenerator.ContainerFromItem method to access the UI elements defined within the DataTemplate that has been applied to the object in the ListBoxItem . 但是,简而言之,您需要使用ItemContainerGenerator.ContainerFromItem方法来访问在DataTemplate中定义的UI元素,该UI元素已应用于ListBoxItem的对象。 Here is a short example from the linked page: 这是链接页面中的简短示例:

// 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ListBoxItem中的复杂UI - Complex UI inside ListBoxItem WPF如何使用数据触发器为listboxitem中的图片设置动画? - WPF how to animate picture inside listboxitem using data trigger? 如何更新ListBoxItem属性 - How to update ListBoxItem property 在WPF中的ListBoxItem上触发触发器时,如何设置父元素的属性 - How to set a property of the parent element, when firing a trigger on ListBoxItem in WPF 如何通过触发另一个数据模板中的动画来对另一个数据模板中定义的元素进行动画处理 - How to animate an element defined inside another data template by triggering the animation from another data template 在ListBoxItem的ControlTemplate中的VisualState中设置Background属性? - Setting Background property inside VisualState in the ControlTemplate of ListBoxItem? 如何将自定义控件的控件模板内定义的元素绑定到(自定义控件类的)子类中定义的属性 - How to bind an element defined inside the control template for a custom control to a property defined in a subclass (of custom control class) 如何以编程方式在ListboxItem上设置IsChecked属性? - How to programmatically set IsChecked property on ListboxItem? 如何获取WPF ListBoxItem属性的值 - How to get value of a property of the WPF ListBoxItem 将ListBoxItem的IsSelected属性传递给DataTemplate内部的UserControl - Passing ListBoxItem's IsSelected property a UserControl inside the DataTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM