我在我的资源文件中定义了一个样式,如下所示
<Style x:Name="ListBoxStyle" TargetType="ListBox" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name,Mode=TwoWay}"
Margin="5"
Foreground="Red">
</TextBlock>
<TextBlock Text="{Binding Age,Mode=TwoWay}"
Margin="5">
</TextBlock>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我不知道在数据模板中放什么
<ListBox x:Name="MyList" ItemsSource="{Binding }">
<ListBox.ItemTemplate>
<DataTemplate>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我尝试使用
<ContentPresenter Style="{StaticResource ListBoxStyle}"></ContentPresenter>
乃至
<ContentControl Style="{StaticResource ListBoxStyle}"></ContentControl>`
但收到此错误
未能分配给属性“System.Windows.FrameworkElement.Style”。
DataTemplate
如果我想提供自定义样式,我应该在标签之间添加什么?