由于某种原因,我无法让UI响应RaisePropertyChanged。好卡住了。 CurrentClient是未触发UI的部分。真奇怪。 希望有人可以帮助我。谢谢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");
}
}
}
我的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" >
答案 0 :(得分:1)
好的不确定您的代码结构,因为我无法使用当前信息重现您的问题。
然而,可能值得您了解的事情,
InSelectedClient(...)
中,参数已经是ClientDetail
类型,使as
强制转换为函数内部。EventToCommand
?您通过ListBox
中的EventToCommand
按住CurrentClient
所选项目。而是直接绑定它。类似的东西:
<ListBox ...
SelectedItem="{Binding CurrentClient}">
...
CurrentClient
相关的任何特定逻辑,并且如果确实没有必要保留它,那么您可以通过绑定TextBox
来摆脱它直接到ListBox
。类似
<TextBox Text="{Binding ElementName=lbClientList, Path=SelectedItem.CM_Address1}" />
我猜CM_Address1
是ClientDetail
类
现在所有这些方法对我来说都很好。如果这些都不适合您,我建议将一个可重现的独立示例放在一起。如果你愿意,我可以在演示中附上这些方法的样本(如果你的代码结构不同,我不确定它会有多大帮助)