我是WPF的新手,整个绑定和样式设计世界都令人困惑。
我为ListViewItem创建了默认ControlTemplate的副本:
<Window.Resources>
<ControlTemplate x:Key="ListViewItemControlTemplate1" TargetType="{x:Type ListViewItem}">
...
</ControlTemplate>
</Window.Resources>
然后我的ListView中有一些列,例如:
<ListView x:Name="lstFiles">
<ListView.View>
<GridView>
<GridViewColumn x:Name="clmName" Header="Name" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
如果在Designer中添加新项目,则可以轻松应用该ControlTemplate:
<ListViewItem Content="ListViewItem1" Template="{DynamicResource ListViewItemControlTemplate1}"/>
但是当使用lstFiles.ItemsSource
将项目从代码绑定到ListView时,如何使用该模板?
答案 0 :(得分:0)
我认为您想覆盖listviewitem的样式。来自:How to override ListViewItem style?
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource MaterialDesignListBoxItem}">
<Style.Resources>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ListBox.xaml" />
</Style.Resources>
<EventSetter Event="PreviewMouseLeftButtonDown"
Handler="ListViewItem_PreviewMouseLeftButtonDown" />
</Style>