Silverlight列表框自定义样式
我在我的资源文件中定义了一个样式,如下所示Silverlight列表框自定义样式,silverlight,listbox,contentcontrol,Silverlight,Listbox,Contentcontrol,我在我的资源文件中定义了一个样式,如下所示 <Style x:Name="ListBoxStyle" TargetType="ListBox" > <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBox"> <St
<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标记之间添加什么?试试:
<ListBox x:Name="MyList" ItemsSource="{Binding }">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name,Mode=TwoWay}"
Margin="5"
Foreground="Red">
</TextBlock>
<TextBlock Text="{Binding Age,Mode=TwoWay}"
Margin="5">
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这可以解决你的问题
如果定义样式,则定义列表框的外观,如背景、前景等。。。。
您可以在此处获得默认样式:http://msdn.microsoft.com/en-us/library/cc278062v=vs.95.aspx
ItemTemplate它是一个DataTemplate,它定义了列表中单个元素的数据表示形式如何使用绑定等等
如果要为单个元素定义样式,如鼠标悬停、聚焦、。。。您可以为ListBoxItems编写样式。您可以通过ItemContainerStyle将其添加到列表框中
<ListBox ItemContainerStyle="{StaticResource YourResourceKey}"/>
你想完成什么?您已经为ListBox定义了一个样式,并且正在尝试将其应用于ContentPresenter和ContentControl。这不对。我想在另一个资源文件中定义列表框的样式,在我的page.xaml中只引用该样式。正确的方法是什么?