【问题标题】:SelectedItem property not updatingSelectedItem 属性未更新
【发布时间】:2012-03-15 23:37:57
【问题描述】:

对于 C# 和 MVVM 来说相对较新,但我正在使用 MVVM Light Toolkit 制作一个 WP7 应用程序。我在 ListBox 中对属性进行双向绑定时遇到问题。我有一个 ObservableCollection 客户端,我正在尝试选择一个单独的客户端(单击它时会将我带到一个新的 ViewModel)。

当我单击一个选定的项目时,它应该更新 SelectedItem 属性并将值设置为单击的客户端。但是,当单击它时,它甚至没有到达设置器(我用 * 标记了断点)。有谁知道我哪里出错了或者有更好的解决方案?我已经在这个地方拖了好几个小时了!

XAML 标记:

        <ListBox SelectedItem="{Binding SelectedItem, Mode=TwoWay}" ItemsSource="{Binding ClientList, Mode=TwoWay}" x:Name="FirstListBox" Margin="0,0,-12,0" ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,17" Width="432">
                    <Button CommandParameter="{Binding}">
                        <helper:BindingHelper.Binding>
                            <helper:RelativeSourceBinding Path="ShowClientCommand" TargetProperty="Command"
                                    RelativeMode="ParentDataContext" />
                        </helper:BindingHelper.Binding>
                        <Button.Template>
                            <ControlTemplate>
                                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

ViewModel 属性:

    public ObservableCollection<Client> ClientList
    {
        get 
        {
            return _clientList;
        }
        set 
        {
            _clientList = value;
            RaisePropertyChanged("ClientList");
        }
    }

    public Client SelectedItem
    {
        get
        {
            return _selectedItem;
        }
        set
        {
         *   _selectedItem = value;
            RaisePropertyChanged("SelectedItem");
        }
    }

【问题讨论】:

  • 为什么在 ListBox 模板中使用按钮?我不确定,但可能 SelectedItem 不会发生,因为 Button click 事件首先发生!
  • 按照 MVVM 模式中的列表框使用按钮的教程,这使我想到了这一点。你确实是对的!

标签: mvvm windows-phone-7 binding selecteditem two-way-binding


【解决方案1】:

可能是因为您没有注册 selection_changed 事件,所以它没有更改属性?

我不完全确定为什么这不起作用,但这是我一直使用且模板推荐的解决方案。

像这样为您的列表框注册 SelectionChanged 事件:

<ListBox SelectionChanged="FirstListBox_SelectionChanged" ItemsSource="{Binding ClientList, Mode=TwoWay}" x:Name="FirstListBox" Margin="0,0,-12,0" ScrollViewer.VerticalScrollBarVisibility="Auto">

然后在相应的 .cs 文件中,有一个如下所示的处理程序:

private void FirstListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // If selected index is -1 (no selection) do nothing
    if (FirstListBox.SelectedIndex == -1)
        return;

    // get the client that's selected
    Client client = (Client) FirstListBox.selectedItem;

    //... do stuff with the client ....

    // reset the index (note this will fire this event again, but
    // it'll automatically return because of the first line
    FirstListBox.SelectedIndex = -1;
}

【讨论】:

  • 嗯,当我去掉我已经实现的按钮逻辑并使用 selectionChanged 时,这有效。我不得不放弃这个解决方案的 MVVM 模式,但我会寻找一种方法将它和 SelectionChanged 事件结合起来。感谢您的回复 - 它让我再次上路。 :)
  • 是的,您发布的代码看起来应该可以工作,但我对 MVVM 模式的了解不够,无法提供帮助,所以我只是告诉您我知道的工作。但我很高兴你在滚动:)
猜你喜欢
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多