我在WPF项目中有这个XAML。
<ListView Grid.Column="0" Grid.Row="3" Grid.RowSpan="4" Grid.ColumnSpan="2" ItemsSource="{Binding FilteredApps}" SelectedItem="{Binding SelectedApp, Mode=TwoWay}">
<ListView.View>
<GridView>
<GridViewColumn Header="Status">
<GridViewColumn.CellTemplate >
<DataTemplate DataType="{x:Type Image}">
<Image>
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding State, Mode=TwoWay}" Value="{x:Static common:Globals+ModelState.Unedited}">
<Setter Property="Source" Value="../Resources/IndraDoc/AppUnedited.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding State, Mode=TwoWay}" Value="{x:Static common:Globals+ModelState.Edited}">
<Setter Property="Source" Value="../Resources/IndraDoc/AppEdited.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="{x:Static common:Globals+ModelState.New}">
<Setter Property="Source" Value="../Resources/IndraDoc/AppNew.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="{x:Static common:Globals+ModelState.Deleted}">
<Setter Property="Source" Value="../Resources/IndraDoc/AppDeleted.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Namn" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
当财产&#34;姓名&#34;更改相应的更改显示在Grid中,但是当属性&#34; State&#34;更改图像未切换。有什么需要吗?
修改 奇怪的是,我编辑了XAML,并将ListView中的图像添加到SelectedApp,但这样工作正常,但这不是预期的功能。它现在可以使用,但我仍然很好奇为什么我的原始代码不会触发。
添加我执行statechange的代码:
private void ChangeStateOnSelected(Globals.ModelState newState)
{
if (SelectedApp.State != newState) //dont do any changework if new state is same as current sate
{
if (SelectedApp.State == Globals.ModelState.New && newState == Globals.ModelState.Edited) //this is not allowed
return;
SelectedApp.State = newState;
if (newState == Globals.ModelState.Unedited)
{
var selectedId = SelectedApp.Id;
_allApps.Replace(_allApps.FirstOrDefault(x => x.Id == SelectedApp.Id), SelectedApp);
_selectedApp = (Data.DataModel.App)_locationService.GetApp(selectedId);
_filteredApps.Replace(_filteredApps.FirstOrDefault(x => x.Id == SelectedApp.Id), SelectedApp);
}
NotifyOfPropertyChange(() => SelectedApp);
NotifyOfPropertyChange(() => AllApps);
NotifyOfPropertyChange(() => FilteredApps);
}
}