【问题标题】:Select the "old value" in a listbox in a edit page在编辑页面的列表框中选择“旧值”
【发布时间】:2011-01-18 23:14:44
【问题描述】:

在我的应用程序中,我有一个保存/编辑页面。 当前流程如下:用户有一个主页,其中包含元素列表。他可以点击“添加”按钮,进入“添加”页面,他可以在其中输入信息并存储。一旦他这样做,信息就会保存并显示在列表中。 如果他在列表中单击,他会移动到“编辑”页面,他可以在其中更改信息。

实际上,添加页面和编辑页面是相同的,第二个页面填充了字段,而第一个没有。

我在此页面中有 3 个列表框,一个用于严重性,一个用于类别,一个用于报告者。此信息在保存前在列表框中选择,在编辑阶段应自动选择,因此用户知道“旧”值。 为了自动选择值,我尝试了两种方法:

1-在我的 xaml 中:

<ListBox Height="103" Name="lbSeverities" Width="439" HorizontalAlignment="Left" Margin="20,0,0,0" SelectionMode="Single" ItemsSource="{Binding Severities}" DisplayMemberPath="Name" SelectedItem="{Binding Task.Severity}"/>

而且我还将 Severity 类的 Equals 方法重写为合理的实现。

2- 在我的 xaml 中

<ListBox Height="103" Name="lbSeverities" Width="439" HorizontalAlignment="Left" Margin="20,0,0,0" SelectionMode="Single" ItemsSource="{Binding Severities}" DisplayMemberPath="Name" SelectedIndex="{Binding Task.Severity, Converter={StaticResource SeverityToIndexConverter}}"/>

我使用以下代码创建了 SeverityToIndexConverter:

public class SeverityToIndexConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is Severity)
            {
                Severity currentSeverity = (Severity)value;
                for (int i = 0;  i <   (App.Current as App).MainViewModel.Severities.Count; i++)
                {
                    Severity sev = (App.Current as App).MainViewModel.Severities[i];
                    if (currentSeverity.ID == sev.ID)
                        return i;
                }
            }
            return -1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

它们都呈现了相同的结果:有时会自动选择值,但有时不会。选择的时候非常不稳定。 我想象了一些异常,试图抓住它,但我什么也没得到。 我也试过调试,我注意到在案例1中,equals方法被集合的所有成员并行调用,所以我尝试了第二种方法。调试并没有让我得到任何答案。

有没有人遇到过类似的情况? 当用户进入“编辑”页面时,如何使列表框值被选中?

谢谢, 奥斯卡

【问题讨论】:

    标签: silverlight data-binding windows-phone-7


    【解决方案1】:

    我做同样的事情。我的 xaml 看起来像您的第一个示例。

    这是我要做的:

    创建一个名为 Severity 的新属性。在 getter 中返回 Task.Severity。将列表框 SelectedItem 绑定到新属性。

    这也可能是一个时间问题,因此一旦列表框加载了其列表,您可能必须在新属性上调用 NotifyPropertyChanged。

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      相关资源
      最近更新 更多