由于某种原因,我无法让 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_Address1
是 ClientDetail
中的“属性”类(class)现在所有这些方法对我来说都很好。如果这些都不适合您,我建议将一个可重现的独立示例放在一起。如果您愿意,我可以在演示中附上这些方法的示例(不确定如果您的代码结构不同,这会有多大帮助)
关于c# - MVVM Light RaisePropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17142792/