我有一個串列視圖,它的ItemsSource系結到一個專案集合。這些專案通過一個擴展器被分組。我被要求對擴展器的標題進行格式化,以反映有關內容的一些資訊,特別是在member.IsDirty==true的情況下,每個組中是否至少有一個成員。我想根據這個條件來改變Foreground或IsExpanded屬性,這樣人們就可以在串列中找到他們所做的修改,即使在展開器被折疊的情況下(這是最好的默認顯示選項)。
以下是所使用的視窗資源:
<DataTemplate DataType="{x: Type vm:OverViewModel}" x:Key="OV_DT" x:Name="OV_DT">
<control:OverView Width="{Binding ActualWidth,ElementName=ListWidth}"/span>/>。
</DataTemplate>
<CollectionViewSource x:Key="SortedOverViews" Source="{Binding OverViews}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="ParentName"/>
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<屬性組描述 PropertyName="ParentName"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
而串列視圖本身看起來是這樣的:
<ListView ItemsSource="{Binding Source={StaticResource SortedOverViews}}"/span>
ItemTemplate="{StaticResource OV_DT}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="{Binding DataContext.Overviews_IsExpanded,RelativeSource={RelativeSource AncestorType=Window}, Mode=OneTime}">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Items[0].TargetOverView.Goods_Name}"。
Foreground="{Binding Path=Items[0].Is_Intermediate,Converter={StaticResource boolColour}}" />
</StackPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter/>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
我想了一會兒,我需要以某種方式將集合組的內容作為一個集合傳遞給轉換器,回傳類似于myCollection.Where(x=>x.IsDirty).Any()的東西,但我無法解決如何做到這一點,現在我不知道每個組的內容是否甚至可以通過這種方式訪問,以形成一個集合。
如果有任何指導意見,我們將不勝感激!我已經花了相當多的時間來研究。我花了相當多的時間在網上看Expander和ListView的教程,但它們只涵蓋了比較簡單的例子,比如我已經實作的東西。
uj5u.com熱心網友回復:
你可以像這樣系結到Items:
Foreground="{Binding Items, Converter={StaticResource converter}}。
轉換器:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
value is ReadOnlyObservableCollection<object> items
&& items.OfType<YourClass>()?.Any(x => x.IsDirty) == true;
當給定一個完整的集合時,轉換器完美地作業,然而,當串列視圖第一次被填充時,系結只評估了每組中的一個專案
你可以使用一個MultiValueConverter并系結到Items和Items.Count:
<TextBlock ...>
<TextBlock.Foreground>
<MultiBinding Converter="{StaticResource converter}"/span>>
<Binding Path="Items" />
<Binding Path="Items.Count" />
</MultiBinding>
</TextBlock.Foreground>
</TextBlock>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/315694.html
標籤:
