Reputation: 1591
I need to select an already selected Gridview item. But once selected it won't trigger PropertyChanged for the second time. I have tried resetting SelectedIndex to -1 and SelectedItem to null. No luck. Any help would be highly appreciated.
<GridView
x:Name="OrganizationsGridView"
HorizontalAlignment="Center"
ItemContainerStyle="{StaticResource GridViewItemStyle11}"
ItemsSource="{Binding OrganizationsList}"
SelectedIndex="{Binding SelectedIndex, Mode=Twoway}"
SelectedItem="{Binding SelectedOrganization, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}"
Tag="{Binding OrganizationsList}">
Upvotes: 0
Views: 423
Reputation: 2358
You could set SelectedIndex to -1 before the SelectionChanged event occurs, which can trigger PropertyChanged event for a selected item. At the same time, using this method will cause PropertyChanged events to be triggered many times in the process of selecting item.
Please refer to the following code to do this.
XAML code:
<Page
..>
<Grid>
<GridView x:Name="gridview" ItemsSource="{x:Bind persons}"
SelectedIndex="{x:Bind SelectedIndex, Mode=TwoWay}"
IsItemClickEnabled="True"
ItemClick="gridview_ItemClick">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Person">
<TextBlock Text="{x:Bind name}" FontWeight="Bold" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
</Page>
Code behind:
namespace SelectedGridview
{
public sealed partial class MainPage : Page,INotifyPropertyChanged
{
public ObservableCollection<Person> persons { get; set; }
private int _SelectedIndex;
public int SelectedIndex
{
get { return _SelectedIndex; }
set
{
_SelectedIndex = value;
RaisePropertyChanged("SelectedIndex");
}
}
public MainPage()
{
this.InitializeComponent();
persons = new ObservableCollection<Person>
{
new Person(){name="Lily",age=45},
new Person(){name="tom",age=21},
new Person(){name="jack",age=37},
new Person(){name="loean",age=26},
new Person(){name="jojo",age=18}
};
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
private void gridview_ItemClick(object sender, ItemClickEventArgs e)
{
SelectedIndex = -1;
}
}
public class Person
{
public string name { get; set; }
public int age { get; set; }
}
}
Upvotes: 1