我现在尝试了一段时间并在网上搜索但没有成功......现在我敢在stackoverflow上问自己。
因此,目的是分离ItemContainerStyle
的{{1}}和ContentTemplate
的定义。我在ListBoxItem
中定义了ListBoxItemStyle
,在ResourceDictionary
中定义了两个DataTemplates
。
我现在想根据Window.Resources
中定义的样式设置ListBoxItem
的样式,并使用触发器ResourceDictionary
更改DataTemplate
。
我的(不工作)代码所以票价如此:
IsMouseOver
其中<ListBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="20,60,10,10"
Grid.Row="1" Grid.Column="0"
PreviewMouseMove="DragMove_PreviewMouseMove"
PreviewMouseLeftButtonDown="Drag_PreviewMouseLeftButtonDown"
ItemsSource="{Binding Persons}" VerticalContentAlignment="Center"
Style="{StaticResource DefaultListBoxStyle}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Style" Value="{StaticResource DefaultListBoxItemStyle}"/>
<Setter Property="ContentTemplate" Value="{StaticResource PersonsListBoxItemTemplate_default}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource PersonsListBoxItemTemplate_infoButtons}"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
是DefaultListBoxStyle
和ResourceDictionary
&amp;中定义的样式。 PersonsListBoxItemTemplate_default
是PersonsListBoxItemTemplate_infoButtons
中定义的DataTemplates。
现在我收到一个错误,即样式对象不得对其所属对象的style-property产生影响。
我尝试了不同的方法,但没有成功......我还尝试先定义样式然后更改模板,但它也不起作用。
谢谢大家的帮助!
答案 0 :(得分:1)
因此,您无法使用Setter设置样式中的样式属性。为此,您需要使用BasedOn:
<Style TargetType="ListBoxItem" BasedOn="{StaticResource DefaultListBoxItemStyle}">
<Setter ... />
</Style>