XAML在ListBox

时间:2016-09-05 07:15:06

标签: wpf xaml listboxitem itemcontainerstyle contenttemplate

我现在尝试了一段时间并在网上搜索但没有成功......现在我敢在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> DefaultListBoxStyleResourceDictionary&amp;中定义的样式。 PersonsListBoxItemTemplate_defaultPersonsListBoxItemTemplate_infoButtons中定义的DataTemplates。

现在我收到一个错误,即样式对象不得对其所属对象的style-property产生影响。

我尝试了不同的方法,但没有成功......我还尝试先定义样式然后更改模板,但它也不起作用。

谢谢大家的帮助!

1 个答案:

答案 0 :(得分:1)

因此,您无法使用Setter设置样式中的样式属性。为此,您需要使用BasedOn:

<Style TargetType="ListBoxItem" BasedOn="{StaticResource DefaultListBoxItemStyle}">
    <Setter ... />
</Style>