1

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

1 回答 1

1

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

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

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

就像是:

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

就像是

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

我猜CM_Address1ClientDetail课堂上的“财产”

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

于 2013-06-17T11:38:15.893 回答