我有一个类对象UrlCount
的集合:
public ObservableCollection<UrlCount> URLsCount { get; set; }
public class UrlCount
{
public string Url { get; set; }
public int Count { get; set; }
}
此集合为ItemSsource
个ListView
:
<ListView Margin="10" Name="ListUrlView" ItemsSource="{Binding URLsCount}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Url}"/>
<Run Text=" : "/>
<Run Text="{Binding Count}"/>
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
如果Foreground
= -1,我需要更改ListViewItem
中的Count
。我试试这个:
<ListView.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ListUrlView, Path=ItemsSource.Count}" Value="-1">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Resources>
</ListView>
但ListViewItems
没有改变他的Foreground
。我没有任何想法,如何解决它。
答案 0 :(得分:1)
您需要在DataTrigger
的{{1}}中定义ListView
,如下所示:
ItemContainerStyle
<强>更新强>
不要忘记设置 <ListView Margin="10" Name="ListUrlView" ItemsSource="{Binding UrLsCount}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Url}"/>
<Run Text=" : "/>
<Run Text="{Binding Count}"/>
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Count}" Value="-1">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
DataContext
并实现DataContext="{Binding RelativeSource={RelativeSource Self}}"
接口,这里是完整示例的代码隐藏:
INotifyPropertyChanged
结果