2010-09-30 78 views
1

我遇到了一个我无法解决的问题。我希望在这里找到答案。当某个listboxitem被选中时,我需要一个列表框来隐藏一半的路。我设置了不透明蒙板动画的故事板,它可以很好地融合在一起。我的问题我无法启动BeginStoryboard。我尝试了很多方法,但没有成功。我需要隐藏列表框来显示它背后的内容。我从XML数据文件生成listboxitems,并基于我计划启动故事板播放的名称节点。如何根据ListBoxItem选择开始故事板?

在这里,我有。 我创建的DataTemplate我在ListBoxItem的样式设置:

<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”对象不能是找到。我明白,列表框对象不能从数据模式级别获得。我想知道什么是正确的解决方案,使动画可以播放和最终删除点击其他listboxitem。先谢谢你。

+0

这是为什么在社区维基上? – Val 2010-09-30 10:49:31

回答

1

我把一些东西放在一起,希望能帮助你(新的默认WPF应用程序,MainWindow的DataContext设置为自己)。我最终使用了一个IValueConverter来将生成的XmlLinkedNode中的Name从ListBox的SelectedItem中取出,但是应该有一种更优雅的方式来使用我不熟悉的XPath语句。基本上宣告你的故事板的列表框样式,而不是在DataTemplate中:

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> 
+0

谢谢你的帮助。不幸的是,我geting错误“标签'SelectionConverter'在XML命名空间中不存在,即使我添加了它 – vladc77 2010-09-30 12:52:38

+0

您是否已将SelectionConverter类添加到您在XAML中定义的命名空间 – MrDosu 2010-09-30 13:01:44

+0

是的,我复制了转换器类,粘贴到MainWindow.xaml.cs,然后我添加命名空间 - xmlns:local =“clr-namespace:WpfApplication1”。它真的是starnge。它应该工作。我检查所有命名多次,但我得到相同的错误VS和Blend – vladc77 2010-09-30 13:32:51