C# 如何在不执行ValueChanged事件的情况下设置ListPicker的索引
这个问题简直让我发疯。我有一个ListPicker,它动态地填充了几个项目。我已将声明的SelectionChanged事件处理程序放置在页面的已加载事件中。当用户单击页面上的某个项目时,ListPicker可见性将从Collpased切换到Visible,我设置ListPicker的值。问题是,ListPicker的索引将基于用户设置,因此在三个项目中,当前索引可能是1,而不是默认的0。我需要在ListPicker中将1显示为当前项,而不触发SelectionChanged事件(它根据当前索引执行操作)。然后,也只有在用户自己更改所选项目时,我才需要触发SelectionChanged事件 这样做的主要原因不仅是当显示列表选择器时,用户需要在列表选择器中查看他或她的当前设置,而且在SelectionChanged事件上会发生操作,这些操作会覆盖当前存在的内容,这非常混乱,除非用户指定,否则不应该发生 我目前的情况如下 XAMLC# 如何在不执行ValueChanged事件的情况下设置ListPicker的索引,c#,xaml,windows-phone-8,listpicker,C#,Xaml,Windows Phone 8,Listpicker,这个问题简直让我发疯。我有一个ListPicker,它动态地填充了几个项目。我已将声明的SelectionChanged事件处理程序放置在页面的已加载事件中。当用户单击页面上的某个项目时,ListPicker可见性将从Collpased切换到Visible,我设置ListPicker的值。问题是,ListPicker的索引将基于用户设置,因此在三个项目中,当前索引可能是1,而不是默认的0。我需要在ListPicker中将1显示为当前项,而不触发SelectionChanged事件(它根据当前索引
XAML.CS
private void Page_Loaded(object sender, RoutedEventArgs e)
{
lp.SelectionChanged += lp_SelectionChanged;
}
void EditableEllipse_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (sender != null)
{
DependencyObject tappedElement = e.OriginalSource as UIElement;
// find parent UI element of type PhotoThumbnail
//PhotoThumbnail i = this.FindParentOfType<PhotoThumbnail>(tappedElement);
i = this.FindParentOfType<PhotoThumbnail>(tappedElement);
if (i != null)
{
BuildControl(i);
}
}
}
private void BuildControl(PhotoThumbnail pp)
{
switch(pp.NName)
{
case "flip":
List<ListPickerItem> l = new List<ListPickerItem>();
l.Add(new ListPickerItem { Name = "Flip_Vertical", Content = AppResources.App_Flip_Vertical });
l.Add(new ListPickerItem { Name = "Flip_Horizontal", Content = AppResources.App_Flip_Horizontal });
l.Add(new ListPickerItem { Name = "Flip_Both", Content = AppResources.App_Flip_Both });
lp.ItemsSource = l; //Code execution jumps from here to ValueChanged event immediately
lp.Visibility = Visibility.Visible;
lp.SelectedIndex = Settings.Flip.Value - 1;
break;
..
}
}
private async void lp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lp.SelectedIndex != -1) //always defaults to = 0
{
var item = (sender as ListPicker).SelectedItem;
string name = ((ListPickerItem)item).Name;
if (name != null)
{
switch (name)
{
case "Flip_Vertical":
Settings.Flip.Value = 1;
..perform process based on current Setting.Flip.Value.. break;
case "Flip_Horizontal":
Settings.Flip.Value = 2;
..perform process based on current Setting.Flip.Value..
break;
case "Flip_Both":
Settings.Flip.Value = 3;
..perform process based on current Setting.Flip.Value..
break;
...
}
}
}
private void页面\u已加载(对象发送方,路由目标)
{
lp.SelectionChanged+=lp_SelectionChanged;
}
void EditableEllipse_点击(对象发送器,System.Windows.Input.GestureEventArgs e)
{
if(发送方!=null)
{
DependencyObject tappedElement=e.OriginalSource作为UIElement;
//查找PhotoThumbnail类型的父UI元素
//照片缩略图i=此.FindParentOfType(tappedElement);
i=此.FindParentOfType(tappedElement);
如果(i!=null)
{
建筑控制(一);
}
}
}
私有void BuildControl(照片缩略图pp)
{
开关(pp.NName)
{
案例“翻转”:
列表l=新列表();
l、 添加(新ListPickerItem{Name=“Flip_Vertical”,Content=AppResources.App_Flip_Vertical});
l、 添加(新ListPickerItem{Name=“Flip_Horizontal”,Content=AppResources.App_Flip_Horizontal});
l、 添加(新ListPickerItem{Name=“Flip_-Both”,Content=AppResources.App_-Flip_-Both});
lp.ItemsSource=l;//代码执行立即从这里跳到ValueChanged事件
lp.Visibility=可见性.Visibility;
lp.SelectedIndex=Settings.Flip.Value-1;
打破
..
}
}
私有异步无效lp_SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
if(lp.SelectedIndex!=-1)//始终默认为=0
{
变量项=(发送方作为列表选择器)。选择EdItem;
字符串名称=((ListPickerItem)项).name;
if(name!=null)
{
交换机(名称)
{
案例“垂直翻转”:
Settings.Flip.Value=1;
..根据当前设置执行处理。翻转。值..中断;
案例“水平翻转”:
Settings.Flip.Value=2;
..根据当前设置执行处理。翻转。值。。
打破
案例“翻转两个”:
Settings.Flip.Value=3;
..根据当前设置执行处理。翻转。值。。
打破
...
}
}
}
尝试使用操作顺序技巧(通过取消勾选和重新勾选事件)
private void BuildControl(照片缩略图pp)
{
开关(pp.NName)
{
案例“翻转”:
//脱钩事件
lp.SelectionChanged-=lp_SelectionChanged;
列表l=新列表();
l、 添加(新ListPickerItem{Name=“Flip_Vertical”,Content=AppResources.App_Flip_Vertical});
l、 添加(新ListPickerItem{Name=“Flip_Horizontal”,Content=AppResources.App_Flip_Horizontal});
l、 添加(新ListPickerItem{Name=“Flip_-Both”,Content=AppResources.App_-Flip_-Both});
lp.ItemsSource=l;//代码执行立即从这里跳到ValueChanged事件
lp.Visibility=可见性.Visibility;
lp.SelectedIndex=Settings.Flip.Value-1;
//设置索引后,重新挂钩事件
lp.SelectionChanged+=lp_SelectionChanged;
打破
..
}
}
有趣的是,在最后一次选择阅读这篇文章之前,我尝试了同样的方法,效果很好。您是否建议将lp.SelectionChanged+=lp\u SelectionChanged
从Page\u Loaded
中删除?@Matthew这取决于加载页面后您是否还有其他需要触发的内容,如果没有,则可以安全地删除它。
private void Page_Loaded(object sender, RoutedEventArgs e)
{
lp.SelectionChanged += lp_SelectionChanged;
}
void EditableEllipse_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (sender != null)
{
DependencyObject tappedElement = e.OriginalSource as UIElement;
// find parent UI element of type PhotoThumbnail
//PhotoThumbnail i = this.FindParentOfType<PhotoThumbnail>(tappedElement);
i = this.FindParentOfType<PhotoThumbnail>(tappedElement);
if (i != null)
{
BuildControl(i);
}
}
}
private void BuildControl(PhotoThumbnail pp)
{
switch(pp.NName)
{
case "flip":
List<ListPickerItem> l = new List<ListPickerItem>();
l.Add(new ListPickerItem { Name = "Flip_Vertical", Content = AppResources.App_Flip_Vertical });
l.Add(new ListPickerItem { Name = "Flip_Horizontal", Content = AppResources.App_Flip_Horizontal });
l.Add(new ListPickerItem { Name = "Flip_Both", Content = AppResources.App_Flip_Both });
lp.ItemsSource = l; //Code execution jumps from here to ValueChanged event immediately
lp.Visibility = Visibility.Visible;
lp.SelectedIndex = Settings.Flip.Value - 1;
break;
..
}
}
private async void lp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lp.SelectedIndex != -1) //always defaults to = 0
{
var item = (sender as ListPicker).SelectedItem;
string name = ((ListPickerItem)item).Name;
if (name != null)
{
switch (name)
{
case "Flip_Vertical":
Settings.Flip.Value = 1;
..perform process based on current Setting.Flip.Value.. break;
case "Flip_Horizontal":
Settings.Flip.Value = 2;
..perform process based on current Setting.Flip.Value..
break;
case "Flip_Both":
Settings.Flip.Value = 3;
..perform process based on current Setting.Flip.Value..
break;
...
}
}
}
private void BuildControl(PhotoThumbnail pp)
{
switch(pp.NName)
{
case "flip":
// unhook event
lp.SelectionChanged -= lp_SelectionChanged;
List<ListPickerItem> l = new List<ListPickerItem>();
l.Add(new ListPickerItem { Name = "Flip_Vertical", Content = AppResources.App_Flip_Vertical });
l.Add(new ListPickerItem { Name = "Flip_Horizontal", Content = AppResources.App_Flip_Horizontal });
l.Add(new ListPickerItem { Name = "Flip_Both", Content = AppResources.App_Flip_Both });
lp.ItemsSource = l; //Code execution jumps from here to ValueChanged event immediately
lp.Visibility = Visibility.Visible;
lp.SelectedIndex = Settings.Flip.Value - 1;
// after setting the index, rehook the event
lp.SelectionChanged += lp_SelectionChanged;
break;
..
}
}