列表框选择生成无法动画不可变对象错误

时间:2011-01-19 12:08:24

标签: c# wpf xaml .net-4.0

我有一个WPF 4应用程序。它包含2个列表框。

第一个有可用匹配列表。选择匹配时,选择将用作显示匹配详细信息的详细信息网格的DataContext。此外,还会播放一个故事板,第二个列表框会显示可用的市场。

一切正常。

问题是当从第二个列表框中选择市场时,应用程序崩溃了,我得到:

  

InvalidOperationException是   未处理的

     

无法设置'(0)。(1)'的动画   不可变对象实例

我不知道为什么会这样,因为市场列表框不会尝试播放任何动画。它会干扰匹配列表中的选择吗? 当我在MarketsList中进行选择时,我尝试重置详细信息datacontext但是没有用。

这是两个ListBoxes的xaml

       <StackPanel x:Name="Connected" Grid.Column="1" Grid.Row="1" Visibility="Collapsed">
            <ListBox x:Name="ListBoxMatches" HorizontalAlignment="Left" VerticalAlignment="Center" ItemContainerStyle="{DynamicResource ListBoxItemContainerStyle1}" ItemTemplate="{DynamicResource MatchesDataTemplate}" SelectionChanged="ListBoxMatches_SelectionChanged" />
        </StackPanel>

        <Border x:Name="border1" BorderBrush="White" BorderThickness="1" Grid.Row="1" CornerRadius="20" Padding="15" Margin="-400,0,400,0">
            <StackPanel x:Name="Markets">
                <ListBox x:Name="ListBoxCurrentMarkets" SelectionChanged="ListBoxCurrentMarkets_SelectionChanged" />
            </StackPanel>
        </Border>

这是ListBoxMatches的SelectionChanged事件的代码:

 private void ListBoxMatches_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ListBoxMatches.SelectedIndex != -1)
            {
                var selectedMatch = (Match)ListBoxMatches.SelectedItem;
                if (MatchData.DataContext == null)
                {
                    MatchData.DataContext = selectedMatch;
                    Storyboard PlayMatch = (Storyboard)FindResource("MatchSelected");
                    BeginStoryboard(PlayMatch);
                }
                else if (MatchData.DataContext != selectedMatch)
                {
                    MatchData.DataContext = selectedMatch;
                }
            }
        }

如果您需要更多代码(如故事板xaml),请告诉我。

如果有人知道原因,请问有什么帮助。

由于

更新

我做了丹尼尔推荐的改变,但我一定做错了,因为它不起作用。 这是我设置项目来源的代码:

 List<Market> TestList = new List<Market>();
                Market market = new Market { ID = 0, MarketName = "CorrectScore" };
                TestList.Add(market);
                market = new Market { ID = 1, MarketName = "Match Odds" };
                TestList.Add(market);                
                ListBoxCurrentMarkets.ItemsSource = TestList;    

这是列表框的xaml

<ListBox x:Name="ListBoxCurrentMarkets" SelectionChanged="ListBoxCurrentMarkets_SelectionChanged" ItemContainerStyle="{DynamicResource ListBoxItemContainerStyle1}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <TextBlock x:Name="Market" Foreground="Black" TextWrapping="Wrap" Text="{Binding MarketName, Converter={x:Static local:MyCloneConverter.Instance}}" />
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

这是CloneConverter:

public class MyCloneConverter : IValueConverter
    {
        public static MyCloneConverter Instance = new MyCloneConverter();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Freezable)
            {
                value = (value as Freezable).Clone();
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

    }

请看一看并告诉我你的想法?我在valueconverter中设置了一个断点,它没有被击中。此外,列表框中的项目仅在选择发生错误时显示。 感谢

更新

这是动画的xaml。动画将2个负边距边框移动到正边距(即从屏幕外进入屏幕)。有一个边框包含MatchDetails(文本块数据绑定到匹配),有问题的边框是包含可用市场ListBox的边框。

<Storyboard x:Key="MatchSelected">
            <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="borderMatchData">
                <EasingThicknessKeyFrame KeyTime="0" Value="0,-400,0,400"/>
                <EasingThicknessKeyFrame KeyTime="0:0:1" Value="0"/>
            </ThicknessAnimationUsingKeyFrames>
            <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="borderMarkets">
                <EasingThicknessKeyFrame KeyTime="0" Value="-400,0,400,0"/>
                <EasingThicknessKeyFrame KeyTime="0:0:1" Value="0"/>
            </ThicknessAnimationUsingKeyFrames>
        </Storyboard>

这是从MatchesListBox的selectionchanged事件播放故事板的代码:

MatchData.DataContext = selectedMatch;
                    Storyboard PlayMatch = (Storyboard)FindResource("MatchSelected");
                    BeginStoryboard(PlayMatch);

这是BorderMArkets的xaml:

<Border x:Name="borderMarkets" BorderBrush="White" BorderThickness="1" Grid.Row="1" CornerRadius="20" Padding="15" Margin="-400,0,400,0">
            <StackPanel x:Name="Markets">
                <ListBox x:Name="ListBoxCurrentMarkets" SelectionChanged="ListBoxCurrentMarkets_SelectionChanged" ItemContainerStyle="{DynamicResource ListBoxItemContainerStyle1}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <TextBlock x:Name="Market" Foreground="Black" TextWrapping="Wrap" Text="{Binding MarketName, Converter={StaticResource CloneConverter}}" />
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </Border>

更新

这是造成所有问题的模板(我认为)

<ControlTemplate x:Key="ListBoxItemControlTemplate1" TargetType="{x:Type ListBoxItem}">
        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" Background="#FF807F7F">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal"/>
                    <VisualState x:Name="MouseOver">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
                                <EasingColorKeyFrame KeyTime="0" Value="#FF3D3B3B"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Disabled"/>
                </VisualStateGroup>
                <VisualStateGroup x:Name="SelectionStates">
                    <VisualState x:Name="Unselected"/>
                    <VisualState x:Name="Selected">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
                                <EasingColorKeyFrame KeyTime="0" Value="#FFA71616"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="SelectedUnfocused">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
                                <EasingColorKeyFrame KeyTime="0" Value="#FFA71616"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
                <VisualStateGroup x:Name="FocusStates">
                    <VisualState x:Name="Unfocused"/>
                    <VisualState x:Name="Focused">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
                                <EasingColorKeyFrame KeyTime="0" Value="#FF18E526"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="true"/>
                    <Condition Property="Selector.IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            </MultiTrigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

1 个答案:

答案 0 :(得分:3)

请参阅http://blogs.msdn.com/b/mikehillberg/archive/2006/09/26/cannotanimateimmutableobjectinstance.aspx

基本上,如果您为数据绑定属性设置动画,就会发生这种情况。