1

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" >
Joffrey Kern
  • 6,449
  • 3
  • 25
  • 25
scottsanpedro
  • 1,142
  • 2
  • 12
  • 28
  • why is your `ItemsSource` of the `ListBox` set to `ClientView` instead of `ClientDetailsList` – Viv Jun 17 '13 at 08:48
  • well spotted. Though the list was still working. I thought that would have solved it but still the same. When I hit '_currentClient = value;' the correct record is there. thanks for comments – scottsanpedro Jun 17 '13 at 11:02

1 Answers1

1

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)

Viv
  • 17,170
  • 4
  • 51
  • 71
  • Hi Viv. Thanks for this. The eventToCommand is there as I am using a datatemplate. I started with SelectedItem many hours ago and it didn't work. Also your textbox binding isn;t working also. I am beginning to think I have a bug due to changing some references a while back and maybe ended up corrupt. I may well start a new project and get this running and see if that solves it without wasting too much more time. scott – scottsanpedro Jun 17 '13 at 11:49
  • This works via a straight forward listbox but not via a Data template. I believe that to be the problem. Thanks for your time on this – scottsanpedro Jun 17 '13 at 13:27
  • Just for clarity. The textbox binding does also work via a Data Template. – scottsanpedro Jun 17 '13 at 14:10