如何再次 select 一个已经 select 的 gridview 项
How to select an already selected gridview item again
我需要 select 一个已经 select 的 Gridview 项目。但是一旦 selected 它就不会第二次触发 PropertyChanged。我尝试将 SelectedIndex 重置为 -1 并将 SelectedItem 重置为 null。没有运气。
任何帮助将不胜感激。
<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}">
您可以在SelectionChanged事件发生前将SelectedIndex设置为-1,这样可以触发选中项的PropertyChanged事件。同时,使用该方法会导致在选择item的过程中多次触发PropertyChanged事件
请参考以下代码完成此操作。
XAML代码:
<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>
后面的代码:
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; }
}
}
我需要 select 一个已经 select 的 Gridview 项目。但是一旦 selected 它就不会第二次触发 PropertyChanged。我尝试将 SelectedIndex 重置为 -1 并将 SelectedItem 重置为 null。没有运气。 任何帮助将不胜感激。
<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}">
您可以在SelectionChanged事件发生前将SelectedIndex设置为-1,这样可以触发选中项的PropertyChanged事件。同时,使用该方法会导致在选择item的过程中多次触发PropertyChanged事件
请参考以下代码完成此操作。
XAML代码:
<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>
后面的代码:
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; }
}
}