【问题标题】:MVVM Light RaisePropertyChangedMVVM Light RaisePropertyChanged
【发布时间】:2013-06-17 07:56:29
【问题描述】:

由于某种原因,我无法让 UI 响应 RaisePropertyChanged。卡住了。 CurrentClient 是不向 UI 触发的部分。真的很奇怪。 希望有人可以帮助我。谢谢斯科特

public class ClientViewModel : ViewModelBase
{
    public RelayCommand<ClientDetail> SelectedClient { get; private set; }
    public ICollectionView ClientView { get; private set; }

    private readonly IClients _clientsService;
    public ClientViewModel(IClients clientsService)
    {
        _clientsService = clientsService;
        SelectedClient = new RelayCommand<ClientDetail>(InSelectedClient);
        ClientDetailsList = new ObservableCollection<ClientDetail>(_clientsService.LoadClientList());
        //CurrentClient = ClientDetailsList.ToList().FirstOrDefault();
        ClientView = CollectionViewSource.GetDefaultView(ClientDetailsList);
    }

    private void InSelectedClient(ClientDetail obj)
    {
        CurrentClient = obj as ClientDetail;

    }

    private ObservableCollection<ClientDetail> _clientDetailsList;
    public ObservableCollection<ClientDetail> ClientDetailsList
    {
        get { return _clientDetailsList; }
        set { _clientDetailsList = value; 
            RaisePropertyChanged("ClientDetailsList"); }
    }

    private ClientDetail _currentClient;
    public ClientDetail CurrentClient
    {
        get { return _currentClient; }

        set
        {
            if (_currentClient == value)
            { return; }

            _currentClient = value;
            RaisePropertyChanged("CurrentClient");
        }
    }
}

我的 XAML:

<ListBox Name="lbClientList"
 ItemsSource="{Binding ClientView}" ItemTemplate="{DynamicResource DTClientList}" 
 Background="{x:Null}" BorderThickness="0,0,1,0" BorderBrush="#FF434343" >
<ListBox.Resources>
<DataTemplate x:Key="DTClientList">
    <StackPanel Orientation="Horizontal">
        <TextBlock TextWrapping="Wrap" Text="{Binding CM_CompanyID}" Margin="0,0,5,0" Width="25" Foreground="#FF3590FC"/>
        <TextBlock TextWrapping="Wrap" Text="{Binding CM_CompanyName}" Width="150" Foreground="#FF3590FC"/>
        <TextBlock TextWrapping="Wrap" Text="{Binding CM_MainContact}" Width="100" Foreground="#FF3590FC"/>
    </StackPanel>
    </DataTemplate>
        </ListBox.Resources>
                    <i:Interaction.Triggers>
                     <i:EventTrigger EventName="SelectionChanged">
           <Command:EventToCommand Command="{Binding SelectedClient, Mode=OneWay}" CommandParameter="{Binding SelectedItem, ElementName=lbClientList}"/>
              </i:EventTrigger>
            </i:Interaction.Triggers>
 <ListBox.DataContext>
 <Binding Path="ClientView" Source="{StaticResource ServiceLocator}" UpdateSourceTrigger="PropertyChanged"/>
        </ListBox.DataContext>
  </ListBox>

在调试中,代码正在点击 RaiseProertyChange,但我的 UI 上什么也没看到

<TextBox Text="{Binding CurrentClient.CM_Address1}" TextWrapping="Wrap" VerticalAlignment="Center" Width="210" Background="{DynamicResource MainBackgrouund}" BorderThickness="0,0,0,1" >

【问题讨论】:

  • 为什么ListBox 中的ItemsSource 设置为ClientView 而不是ClientDetailsList
  • 很好看。尽管该列表仍然有效。我以为这会解决它,但还是一样。当我点击'_currentClient = value;'正确的记录在那里。感谢 cmets

标签: c# wpf mvvm listbox mvvm-light


【解决方案1】:

好吧,我不太确定你的代码结构,因为我无法用当前信息重现你的问题。

但是有几件事可能值得您了解,

  • InSelectedClient(...) 中,参数已经是 ClientDetail 类型,使得函数内部的 as 强制转换是多余的。
  • 接下来,您为什么还要为此使用EventToCommand?您通过CurrentClient 中的EventToCommand 持有ListBox 所选项目。而是直接绑定它。

类似:

<ListBox ...
         SelectedItem="{Binding CurrentClient}">
...
  • 最后,如果您在 VM 中没有任何与 CurrentClient 相关的特定逻辑,并且确实没有必要保留它,那么您可以通过将 TextBox 直接绑定到 @ 来摆脱它987654331@.

类似

<TextBox Text="{Binding ElementName=lbClientList, Path=SelectedItem.CM_Address1}" />

我猜CM_Address1ClientDetail 类中的“属性”

现在所有这些方法对我来说都很好。如果这些都不适合您,我建议将一个可重复的独立示例放在一起。如果您愿意,我可以在演示中附上这些方法的示例(不确定如果您的代码结构不同,这是否会有很大帮助)

【讨论】:

  • 嗨,万岁。谢谢你。当我使用数据模板时,eventToCommand 就在那里。几个小时前,我从 SelectedItem 开始,但它没有用。此外,您的文本框绑定也不起作用。我开始认为我有一个错误,因为不久前更改了一些引用并且可能最终损坏了。我很可能会开始一个新项目并运行它,看看是否能解决它而不会浪费太多时间。斯科特
  • 这通过一个简单的列表框而不是通过数据模板工作。我相信这是问题所在。感谢您抽出宝贵时间
  • 只是为了清楚起见。文本框绑定也可以通过数据模板工作。
猜你喜欢
  • 2013-08-09
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 2017-01-17
  • 1970-01-01
  • 2020-04-18
  • 2014-08-29
相关资源
最近更新 更多