【问题标题】:How to begin storyboard based on ListBoxItem selection?如何根据 ListBoxItem 选择开始情节提要?
【发布时间】:2011-04-19 07:00:13
【问题描述】:

我遇到了一个我无法解决的问题。我希望能在这里找到答案。 I need a listbox to hide half way when a certain listboxitem is selected.我设置了一个带有不透明蒙版动画的故事板,可以很好地混合。我的问题我无法启动 BeginStoryboard。我尝试了很多方法,但都没有成功。我需要隐藏列表框以显示其背后的内容。我从 XML 数据文件生成列表框项,并根据我计划启动情节提要播放的名称节点。

这就是我所拥有的。 我创建了我在 ListBoxItem 样式中设置的 DataTemplate:

<DataTemplate x:Key="SelectedListBoxItemDataTemplate">
        <StackPanel x:Name="DataItemSelected" Orientation="Horizontal" Margin="12,0,0,0" >
                <TextBlock FontFamily="Arial" Text="►" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="{Binding XPath=state}" Margin="-4, 0,6,4"/>
                <Image x:Name="ListBoxImage" Source="{Binding XPath=icon}" Margin="4,4,14,4" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Uniform" />
                <TextBlock x:Name="textBlock" Text="{Binding XPath=name}" LineHeight="22" Foreground="#FFFFFFFF" FontSize="16"  />  
                <Border x:Name="PART_Icon" Background="{x:Null}" Width="{Binding NodeValue.Width}" HorizontalAlignment="Left" Padding="3,0"></Border>
            </StackPanel>

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding XPath=name}" Value="SERVERS">
                <Setter TargetName="PART_Icon" Property="Background" Value="Black" />
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource HideListBox}" x:Name="HideListBox_BeginStoryboard"/>
                </DataTrigger.EnterActions>  
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate> 

我需要运行我保存在 Window.Resources 中的这个故事板:

<Storyboard x:Key="HideListBox">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[0].(GradientStop.Offset)" Storyboard.TargetName="Nav_ListBox">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.069"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[1].(GradientStop.Offset)" Storyboard.TargetName="Nav_ListBox">
            <EasingDoubleKeyFrame KeyTime="0" Value="0.069"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="Nav_ListBox">
            <EasingColorKeyFrame KeyTime="0" Value="White"/>
            <EasingColorKeyFrame KeyTime="0:0:0.4" Value="White"/>
        </ColorAnimationUsingKeyFrames>
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="Nav_ListBox">
            <EasingColorKeyFrame KeyTime="0" Value="#00000000"/>
            <EasingColorKeyFrame KeyTime="0:0:0.4" Value="#00000000"/>
        </ColorAnimationUsingKeyFrames>
        <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.OpacityMask).(LinearGradientBrush.StartPoint)" Storyboard.TargetName="Nav_ListBox">
            <EasingPointKeyFrame KeyTime="0" Value="1.076,0.501"/>
            <EasingPointKeyFrame KeyTime="0:0:0.4" Value="1,0.5"/>
        </PointAnimationUsingKeyFrames>
        <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.OpacityMask).(LinearGradientBrush.EndPoint)" Storyboard.TargetName="Nav_ListBox">
            <EasingPointKeyFrame KeyTime="0" Value="0.035,0.501"/>
            <EasingPointKeyFrame KeyTime="0:0:0.4" Value="0.2,0.5"/>
        </PointAnimationUsingKeyFrames>
    </Storyboard>

我收到无法找到“Nav_ListBox”对象的错误。我知道列表框对象在数据模板级别不可用。我想知道什么是正确的解决方案来启用动画播放并最终在单击其他列表框项时删除。提前谢谢你。

【问题讨论】:

  • 为什么会出现在社区 wiki 上?

标签: wpf binding


【解决方案1】:

我快速整理了一些内容,希望对您有所帮助(新的默认 WPF 应用程序,MainWindow 的 DataContext 设置为自身)。我最终使用 IValueConverter 从 ListBox 的 SelectedItem 中获取生成的 XmlLinkedNode 的名称,但是应该有一种更优雅的方式使用我不熟悉的 XPath 语句。基本上在 ListBoxes 样式中声明您的故事板,而不是在数据模板中:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <XmlDataProvider x:Key="persons"
                         XPath="persons/person"
                         Source="xmldata.xml" />
        <local:SelectionConverter x:Key="selectionConverter" />
    </Window.Resources>

    <Grid>
        <ListBox Background="White" ItemsSource="{Binding Source={StaticResource persons}}" x:Name="lst">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding XPath=name}" />
                        <TextBlock Text="{Binding XPath=prop}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=lst, Path=SelectedItem, Converter={StaticResource selectionConverter}}"
                                     Value="b">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard Duration="0:0:1">
                                        <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                                        To="Green" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                            <DataTrigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard Duration="0:0:1">
                                        <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                                        To="White" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.ExitActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.Style>
        </ListBox>
    </Grid>
</Window>

主窗口代码隐藏:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
    }
}

SelectionConverter.cs

namespace WpfApplication1
{
    public class SelectionConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value == null) ? null : (value as XmlLinkedNode).SelectNodes("name")[0].InnerText;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

示例数据(作为 XML 文件添加到您的项目中):

<?xml version="1.0" encoding="utf-8" ?>
<persons>
    <person>
        <name>a</name>
        <prop>3</prop>
    </person>
    <person>
        <name>b</name>
        <prop>3</prop>
    </person>
    <person>
        <name>c</name>
        <prop>3</prop>
    </person>
</persons>

【讨论】:

  • 感谢您的帮助。不幸的是,我收到错误“XML 命名空间中不存在标签'SelectionConverter',即使我添加了它。
  • 您是否将 SelectionConverter 类添加到您在 XAML 中定义的命名空间
  • 是的,我复制了你的转换器类并粘贴到 MainWindow.xaml.cs,然后我添加了命名空间 - xmlns:local="clr-namespace:WpfApplication1"。这真是太棒了。它应该工作。我多次检查所有命名,但在 VS 和 Blend 中遇到相同的错误
  • 我还添加了对 XML 的引用 - 使用 System.Xml;
  • 我试图避免以这种方式使用转换器:。它仍然没有播放故事板。
猜你喜欢
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多