- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是 XAML 和 C# 的新手,但在使用它的几周内我一直很喜欢使用它。我已经开始开发一个应用程序,并在 XAML 中组合了一个简单的“设置”页面;现在我正在尝试将事件连接到控件以 (a) 在用户与它们交互时更新应用程序状态,以及 (b) 在访问页面时获得当前状态。
不过,我遇到了两个(相关的)障碍:
当我在 XAML 中定义“ListPickerItem”时,工具包:ListPicker 控件似乎无法正常工作,因此在 SettingsPage 构造函数中,我手动设置了内容:
lpColour.ItemsSource = new List<string>()
{
"Red","Blue","Green","Custom…"
};
lpColour.SelectedIndex = 1; // set the currently selected item to "Blue"
但是,因为控件(本例中的 lpColour)在 SelectionChanged 上有一个事件,所以会触发两个事件(一个在填充框时选择“红色”,另一个在选择“蓝色”时)。我现在不想处理“SelectionChanged”;仅当用户自己与控件交互时(例如,如果他们选择“自定义...”,我可以显示一个单独的文本框并提供焦点;但我不想在设置时这样做页面并且他们之前选择了“自定义...”,否则用户会在打开“设置”页面后立即看到键盘...)
我在想也许有某种方法可以从“SelectionChangedEventArgs”或“RoutedEventArgs”中获取事件的“来源”(例如“代码”或“用户输入”)......但也许不是?
我还尝试设置一个“已初始化”的 bool 值(默认情况下为“false”,在构造函数运行后设置为“true”,并将事件处理代码包装在类似“if(已初始化){...}的内容中“;但是在“lpColour.ItemSource=...”和“lpColour.SelectedIndex = 1”代码完成构造函数后,事件似乎仍然被触发,而“初始化”是“假”。很奇怪。 :P
我希望我能解释清楚 - 我以前从未在这里发过帖!
如果您能提供任何帮助,我将不胜感激。谢谢!
更新 - 感谢@MyKuLLSKI 的回答,这给了我一个工作的好地方。
作为旁注,基于这个想法,我尝试将它们最初保留为“List”,并将“IgnoreSelectionChanged”作为一个 int 来“倒计时”(因此在设置 ListPicker 的 ItemSource 之前,我设置了“IgnoreSelectionChanged+ =2”(考虑到将被触发的两个事件);类似地,我在手动设置 SelectedIndex 之前设置了“IgnoreSelectionChanged++”……这似乎也有效。
但是,使用绑定(bind)到 ListPicker 的“ObservableCollection”并依靠它来告知更改似乎比使用 ListPicker 自己的“SelectionChanged”事件更好,因此我将修改我的代码以改为使用它。
再次感谢!
最佳答案
我会尽量回答你所有的问题/问题
您在 XAML 中设置 ItemSource 时遇到问题的原因是因为我几乎可以肯定您遇到了一些绑定(bind)问题。要使绑定(bind)正常工作,您需要在 UIElement 上具有 DataContext 和绑定(bind)。
购买到属性的东西必须是 DependencyProperty 或 INotifyPropertyChanged
另外,List 不是一个很好的集合类型来绑定(bind) ListPicker。相反,您可能希望改用 as ObservableCollextion()。如果此集合绑定(bind)到 ListPicker 并且项目更改,ListPicker 将自动更新。
SelectionChanged 事件被触发 2 次的原因是您对其进行了两次更改。首次创建 ListPicker 时,Selected 项为 null 或 -1,因为其中没有任何项。然后,当您设置 ItemSource 时,它会自动将 SelectedIndex 更改为 0,然后您将其更改为 1。
一种方法是每次您知道用户更改代码中的变量时添加一个标志
Silverlight 缺少 IsLoaded 属性,因此您可能希望在页面加载为真时添加一个 bool 值。
当绑定(bind)不更改 UIElement 中的属性时。而是更改其绑定(bind)到的属性。
以下是我的解决方案,应该可以解决您的所有问题 (WP7.1):
<phone:PhoneApplicationPage
x:Class="WP7Sandbox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
Loaded="PhoneApplicationPageLoaded">
<Grid>
<StackPanel>
<toolkit:ListPicker ItemsSource="{Binding ListPickerCollection, Mode=TwoWay}" SelectionChanged="ListPickerSelectionChanged" SelectedIndex="{Binding ListPickerSelectedIndex, Mode=TwoWay}"/>
<Button Click="ButtonClick" Content="Selection Change and Ignore Event"/>
<Button Click="Button2Click" Content="Selection Change and Trigger Event"/>
<toolkit:ToggleSwitch IsChecked="{Binding ToggleSwitchValue, Mode=TwoWay}"/>
</StackPanel>
</Grid>
</phone:PhoneApplicationPage>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
namespace WP7Sandbox
{
public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private bool IsLoaded;
private bool IgnoreSelectionChanged;
public ObservableCollection<string> ListPickerCollection { get; private set; }
private bool _ToggleSwitchValue;
public bool ToggleSwitchValue
{
get
{
return _ToggleSwitchValue;
}
set
{
_ToggleSwitchValue = value;
OnPropertyChanged("ToggleSwitchValue");
}
}
private int _ListPickerSelectedIndex;
public int ListPickerSelectedIndex
{
get
{
return _ListPickerSelectedIndex;
}
set
{
_ListPickerSelectedIndex = value;
OnPropertyChanged("ListPickerSelectedIndex");
}
}
public MainPage()
{
InitializeComponent();
ListPickerCollection = new ObservableCollection<string>()
{
"Red",
"Blue",
"Green",
"Custom…"
};
}
private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
{
IsLoaded = true;
}
private void ListPickerSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (IsLoaded && !IgnoreSelectionChanged)
{
}
IgnoreSelectionChanged = false;
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
// I want to ignore this SelectionChanged Event
IgnoreSelectionChanged = true;
ChangeListPickerSelectedIndex();
}
private void Button2Click(object sender, RoutedEventArgs e)
{
// I want to trigger this SelectionChanged Event
IgnoreSelectionChanged = false; // Not needed just showing you
ChangeListPickerSelectedIndex();
}
private void ChangeListPickerSelectedIndex()
{
if (ListPickerSelectedIndex - 1 < 0)
ListPickerSelectedIndex = ListPickerCollection.Count - 1;
else
ListPickerSelectedIndex--;
}
}
}
有很多,但应该有帮助
关于c# - WP7 工具包 - 当通过代码(不是用户输入)更改时,如何忽略 ListPicker 的 "SelectionChanged"和 ToggleSwitch 的 "Checked"事件等事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8830588/
我正在尝试设置我的 git 配置,以便我可以使用工作环境和个人环境。 这是我的 ~.gitconfig 文件的内容(碰巧 work 和 private 在 github 上): [url "git@
我有以下情况。我在 Sheet1 上有一个项目列表,我想将项目复制到 Sheet2 并排除特定项目。 假设我在 Sheet1 上有以下项目列表: 我想将“梨”单元格留在 Sheet2 上。 它应该完全
我试图让 gcc 以不同的语言提供错误消息。但它仍然给我英文的错误信息。 我的语言环境输出 varun@varun-desktop:$ 语言环境 LANG=en_IN LC_CTYPE="es_EC.
我在 Linux x86 上使用 gcc。 我的程序将指向 C 函数的指针导出到 LLVM JIT 函数。调用约定是 cdecl。它在 Windows 上的 MingW 上运行良好。但是奇怪的事情发生
windows 上 php 的奇怪问题...我的应用程序加载了一个“核心”文件,该文件加载了一个设置文件、注册自动加载、进行初始化等。在核心文件的顶部我有 include_once("config.p
在工具|选项|调试器选项 |语言异常可以忽略特定的异常类型。是否可以为每个项目定义这个?例如在调试构建配置中(Delphi 2009 和/或 2010)? /编辑:Reported in QC 最佳答
我在一个文本框旁边有 2 个按钮,在这 2 个按钮后面还有另一个文本框。第一个文本框的 tabindex 为 1000,第一个按钮为 1001,第二个按钮为 1002。第二个文本框的 tabindex
我是 python 新手,正在尝试类型提示,但它们似乎只在某些情况下起作用。它们似乎在属性返回类型上按预期工作,但是当我尝试将整数分配给字符串值(即 self._my_string = 4)时,我没有
问题陈述 我有一些国家和这些国家的州的依赖组合框。我使用 VBA 在第一个组合框中填充唯一值,然后在第二个组合框中动态填充唯一值。该代码似乎忽略了初始传递中的条件。 例如,该代码适用于第一个国家/地区
我对 Javascript 有点陌生。我试图做到这一点,以便单击一个页面上的图像会将您带到一个新页面,并在该新页面上显示特定的 div,因此我使用 sessionStorage 来记住并使用 bool
我不确定我是否正确地处理了这个问题。 我有一个 ASP.NET MVC Web 应用程序。有 4 个主要“页面”通过单击菜单选项,可以选择一个页面,并将该页面选项存储在本地存储中。 现在,如果我刷新页
我的页面工作正常,并按预期显示日期和时间,直到我不得不添加 new Date() 以避免 momentjs deprecation warning 。现在我的约会比应有的时间晚了 5 个小时。 我该如
我需要合并一个 fork 项目。不幸的是,CVS $Id 行不同,因此我尝试的合并工具报告所有文件都不同(其中 95% 只有这一行不同) 是否有一个合并工具可以配置为忽略基于模式的行比较结果? [编辑
我是 python 新手,正在尝试类型提示,但它们似乎只在某些情况下起作用。它们似乎在属性返回类型上按预期工作,但是当我尝试将整数分配给字符串值(即 self._my_string = 4)时,我没有
我正在尝试根据 How do a send an HTTPS request through a proxy in Java? 使用代理访问 https 网页 但是我遇到了一个奇怪的问题:HttpsU
我有一个简单的 CMakeLists.txt 文件: cmake_minimum_required(VERSION 2.8.9) project (sample) add_library(Shared
这个问题在这里已经有了答案: typedef pointer const weirdness (6 个答案) 关闭 8 年前。 我有一个结构体 type_s。然后我将指向 struct type_s
我正在尝试制作一个使用 AES 256 加密的应用程序。不幸的是我无法让它工作。也许我没有完全理解密码逻辑。 所以它正在工作,但据我了解,哈希包含密码。但如果我更改密码,输出是相同的。因此,Crypt
我的文件包含一些行,例如 "This is a string." = "This is a string's content." " Another \" example \"" = " New ex
我尝试使用此查询来获取所选健身房的所有用户。 我的问题是查询忽略了这部分:ual.user_id = weekUsers.user_id 查询似乎获取了与我选择的日期匹配的所有用户 ID,而不检查该用
我是一名优秀的程序员,十分优秀!