scottsanpedro
scottsanpedro

Reputation: 1192

MVVM Light RaisePropertyChanged

I am not able to get the UI to respond to RaisePropertyChanged for some reason. Well stuck. CurrentClient is the part not firing to the UI. Really strange. Hopefully someone can help me out. Thanks Scott

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");
        }
    }
}

My 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>

In debug the code is hitting RaiseProertyChange but I am seeing nothing on my UI

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

Upvotes: 1

Views: 3680

Answers (1)

Viv
Viv

Reputation: 17380

Ok not really sure about your code structure since I cannot reproduce your issue with the current info.

However couple things that might be worth you knowing about,

  • In InSelectedClient(...) the argument is already of type ClientDetail making the as cast redundant inside the function.
  • Next why are you even using a EventToCommand for this? you hold the ListBox selected item via the EventToCommand in CurrentClient. Rather just bind it directly.

something like:

<ListBox ...
         SelectedItem="{Binding CurrentClient}">
...
  • Finally If you don't have any specific logic in the VM relating to CurrentClient and if there is really no necessity holding on to it, then you can get rid of it by binding your TextBox directly to the ListBox.

something like

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

I'm guessing CM_Address1 is a "property" in ClientDetail class

Now all these approaches work fine for me. I'd suggest putting together a reproducible stand-alone example if none of these work for you. I can attach a sample of these methods in a demo if you want(not really sure it's gonna be of much help if your code is structured well different)

Upvotes: 1

Related Questions