C# SelectionChanged事件后,无法在LongListSelector中重置网格的可视状态
我实现了C# SelectionChanged事件后,无法在LongListSelector中重置网格的可视状态,c#,visual-studio-2012,windows-phone-8,C#,Visual Studio 2012,Windows Phone 8,我实现了VisualStateManager以突出显示LongListSelector中的选定项 所选项目在SelectionChanged事件期间高亮显示,但问题是当事件完全执行时,所选项目仍高亮显示。即使我离开该页面并返回到原始页面,该项目仍然高亮显示。如果我添加selector.SelectedItem=null在SelectionChanged事件结束时,它再次执行该方法,直到最后抛出未设置为对象实例的对象引用异常 一旦所选项目停用,如何将其视觉状态重置为正常 SelectionChan
VisualStateManager
以突出显示LongListSelector
中的选定项
所选项目在SelectionChanged
事件期间高亮显示,但问题是当事件完全执行时,所选项目仍高亮显示。即使我离开该页面并返回到原始页面,该项目仍然高亮显示。如果我添加selector.SelectedItem=null
在SelectionChanged
事件结束时,它再次执行该方法,直到最后抛出未设置为对象实例的对象引用
异常
一旦所选项目停用,如何将其视觉状态重置为正常
SelectionChanged
事件:
private async void POIS_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<CustomUserControl> userControlList = new List<CustomUserControl>();
GetItemsRecursive<CustomUserControl>(PoiLongListSelector, ref userControlList);
//seleted
if(e.AddedItems.Count > 0 && e.AddedItems[0] != null)
{
foreach (CustomUserControl userControl in userControlList)
{
if (e.AddedItems[0].Equals(userControl.DataContext))
{
VisualStateManager.GoToState(userControl, "Selected", true);
}
}
}
//Unselected
if (e.RemovedItems.Count > 0 && e.RemovedItems[0] != null)
{
foreach (CustomUserControl userControl in userControlList)
{
if (e.RemovedItems[0].Equals(userControl.DataContext))
{
VisualStateManager.GoToState(userControl, "Normal", true);
}
}
}
LongListSelector selector = sender as LongListSelector;
PoiData ld = selector.SelectedItem as PoiData;
string navigateUrl = "";
SystemTray.ProgressIndicator = new ProgressIndicator();
SetProgressIndicator(true);
//CHECK IF RETURNING NULL
SystemTray.ProgressIndicator.Text = "Getting GPS data";
GeoCoordinate coordinate = await GetLocation(ctsPoi.Token);
if (coordinate != null)
{
string passedUrl = GenerateUrl(coordinate, ld.Type);
if (passedUrl != null)
{
SystemTray.ProgressIndicator.Text = "Getting POI data";
string jsonData = await GetJsonDataFromGoogle(passedUrl, ld.Type);
if (jsonData != null)
{
string url = SerializeJsonData(jsonData, ld.Type);
if (url != null)
{
SystemTray.ProgressIndicator.Text = "Done";
navigateUrl = string.Format("/ViewDirection.xaml?serializedData={0}", url);
}
}
}
}
if(navigateUrl != "")
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri(navigateUrl, UriKind.RelativeOrAbsolute));
}
LongListSelector
xaml:
<Grid x:Name="LayoutRoot">
<Grid Margin="0,0,0,15" Grid.Row="0">
<Grid Name="MainGrid" Opacity="1" Visibility="Visible" >
<Grid.Background>
<SolidColorBrush>
<Color>#ff00bfff</Color>
</SolidColorBrush>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Background="{StaticResource PhoneAccentBrush}" Grid.Column="0" Width="65" HorizontalAlignment="Left"
Height="65" Margin="0, 0, 0, 0">
</Grid>
<Grid Grid.Column="1">
<TextBlock Text="{Binding Title}"
FontSize="30" Margin="10,0,0,0"
VerticalAlignment="Center"
Foreground="White"/>
</Grid>
</Grid>
<ProgressBar x:Name="ATMBar" Visibility="Visible"
Opacity="0"
VerticalAlignment="Center"
Margin="0,0,0,0"
IsIndeterminate="True"
Style="{StaticResource CustomIndeterminateProgressBar}" />
</Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Duration="0" To="0.2" Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="MainGrid" />
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="ATMBar" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
<phone:LongListSelector Name="PoiLongListSelector" Margin="12,-20,0,75"
ItemsSource="{Binding Poi.Items}"
SelectionChanged="POIS_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<myUserControl:CustomUserControl />
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
为什么不尝试一些不同的方法来突出显示元素?在选择更改方法之后,可以绑定背景色属性或类似的内容,如果您使用的是mvvm模式,那么您应该具有INotifypropertyChange,因此,它必须将此值更改为其他值,以便绑定到该属性我放弃了此实现,并更改了我希望的工作方式。无论如何,谢谢你的回答:)