你有沒有試過在你的風格中設置這個?
<Style>
<Setter Property="OverridesDefaultStyle" Value="true"/>
</Style
,然後設置模板而不觸發如此完整的代碼是這樣的
<Style TargetType="{x:Type listViewItem}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
</Style>
我之所以列入這種風格是因爲如果你使用你的控件模板內,改變目標類型來{x:Type ListViewItem}
而比targetType="ListViewItem"
它會自動分配這種風格的認爲控制。 也許是因爲我的回答是不完整的人投票我的回答我甚至沒有尊嚴來解釋,哦: 這裏是擴展回答: 除了上面的代碼,你應該在你的Person
類應該添加屬性IsSelected
類應該執行INotifyPropertyChanged並在您的XAML: 「>
現在
在您的視圖模型 你應該有這樣的事情在構造
//lsvProducts is our ListView
view = (CollectionView) CollectionViewSource.GetDefaultView(lsvPeople.ItemsSource);
//in the creation parameter you should put field that you want to group by :-)
PropertyGroupDescription grouping = new PropertyGroupDescription("<FieldToGroupBy>");
view.GroupDescriptions.Add(grouping);
回去XAML
<Style x:Key="GroupedView" TargetType="{x:Type GroupItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=chbx, Path=IsChecked}" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="False">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="16" Foreground="DimGray" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text=" item(s)" FontSize="16" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
和
<!--This goes in the same style as the prevoius sample code -->
<DataTrigger Binding="{Binding ElementName=chbx, Path=IsChecked}" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
注意 當綁定到你可以定義一個在VM說bool IsGrouping
並將其綁定到一個元素。 祝你好運:-)
仍然需要幫助。 – user13657
另外 - 我知道我可以複製DataTemplate並將其複製到選定的控件模板中,但我不想使用此解決方案,因爲每次我在ListView中選擇一個項目時,它都會從我的課程中獲取方法。我需要的只是使用相同的模板更換背景銀。 – user13657
您可以創建一個可重新生成的解決方案嗎?對我們來說呢? – BenjaminPaul