C# 列表框选择产生无法设置不可变对象动画错误
我有一个WPF4应用程序。它包含2个列表框 第一个有可用匹配项的列表。选择匹配时,所选内容将用作显示匹配详细信息的详细信息网格的DataContext。此外,还会播放故事板,第二个列表框将显示可用市场 这一切都很好 问题是当从第二个列表框中选择市场时,应用程序崩溃,我得到: 无效操作异常为 未处理 无法在上设置“(0)。(1)”的动画 不可变对象实例 我不知道为什么会发生这种情况,因为Markets列表框不尝试播放任何动画。它会干扰比赛列表中的选择吗?? 当在MarketsList中进行选择时,我尝试重置details datacontext,但没有成功 这是两个列表框的xamlC# 列表框选择产生无法设置不可变对象动画错误,c#,wpf,xaml,.net-4.0,C#,Wpf,Xaml,.net 4.0,我有一个WPF4应用程序。它包含2个列表框 第一个有可用匹配项的列表。选择匹配时,所选内容将用作显示匹配详细信息的详细信息网格的DataContext。此外,还会播放故事板,第二个列表框将显示可用市场 这一切都很好 问题是当从第二个列表框中选择市场时,应用程序崩溃,我得到: 无效操作异常为 未处理 无法在上设置“(0)。(1)”的动画 不可变对象实例 我不知道为什么会发生这种情况,因为Markets列表框不尝试播放任何动画。它会干扰比赛列表中的选择吗?? 当在MarketsList中进行选择时,
<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>
如果你需要更多的代码(比如故事板xaml),请告诉我
如果有人知道原因和发生了什么,请帮忙
谢谢
更新
我做了Daniel建议的更改,但肯定是做错了什么,因为它不起作用。
这是我设置项目源代码的地方:
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;
请看一看,告诉我你的想法?我在valueconverter中设置了一个断点,它没有被命中。列表框中的项目也会显示,只有在进行选择时才会出现错误。
谢谢
更新
这是动画的xaml。动画将2个负边界移到正边界(即从屏幕外移到屏幕内)。有一个边框包含MatchDetails(文本块数据绑定到匹配项),有问题的边框包含available markets列表框
<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>
这是适用于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>
更新
这就是造成所有问题的模板(我认为)
请参见
基本上,如果对数据绑定属性设置动画,就会发生这种情况。这是因为它试图同时设置动画和绑定?如果我在动画之后应用ListBox项源,那会起作用吗?我不认为这会有帮助。您应该使用本文中介绍的解决方案,即指定一个IValueConverter,它返回任何freezable对象的克隆。示例中的
MyCloneConverter
类也应该可以正常工作。我不确定我做错了什么。我会发布我的更新代码。请看一下,断点肯定会触发。如果将MyCloneConverter
作为资源添加到XAML(
)并引用此(Converter={StaticResource cloneConverter}
),则会发生什么情况?当我这样做时,它将到达断点。当它达到“if(value是Freezable)”时,它将跳过它并返回原始值。是否需要检查值是否可自由释放?值是INPC类中的字符串。
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();
}
}
<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>
MatchData.DataContext = selectedMatch;
Storyboard PlayMatch = (Storyboard)FindResource("MatchSelected");
BeginStoryboard(PlayMatch);
<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>