c# - MVVM Light RaisePropertyChanged

标签 c# wpf mvvm listbox mvvm-light

由于某种原因,我无法让 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" >

最佳答案

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

然而,有几件事可能值得你知道,

  • InSelectedClient(...)参数已经是 ClientDetail 类型制作as在函数内部强制转换冗余。
  • 接下来你为什么还要使用EventToCommand为了这?你持有ListBox通过 EventToCommand 选择项目在 CurrentClient .而是直接绑定(bind)它。

  • 就像是:
    <ListBox ...
             SelectedItem="{Binding CurrentClient}">
    ...
    
  • 最后,如果您在虚拟机中没有任何与 CurrentClient 相关的特定逻辑如果真的没有必要坚持下去,那么你可以通过绑定(bind)你的TextBox来摆脱它。直接到ListBox .

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

    我猜CM_Address1ClientDetail 中的“属性”类(class)

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

    关于c# - MVVM Light RaisePropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17142792/

    相关文章:

    c# - 如何使用 MVVM Light 在 WP7 中设置 StackPanel 的可见性?

    c# - opencv_imgproc.dll 抛出 BadImageFormatException

    c# - MRZ Passport Parser C# 控制台应用程序

    wpf - 在WPF MVVM项目的解决方案资源管理器中移动XAML文件

    c# - SqlException ID 无法绑定(bind)

    c# - 使用单击一次部署时的安装问题

    c# - 使用 IEventAggregator 的 Caliburn Micro Stack 溢出异常

    c# - LINQ 和多对多关系

    c# - 如何编码 Time > 8 :15

    wpf - 将 CheckBox IsChecked 绑定(bind)到 Stackpanel 可见性