Silverlight列表框自定义样式

发布于 2024-10-07 21:58:21 字数 1483 浏览 0 评论 0原文

我在我的资源文件中定义了一个样式,如下所示,

   <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 标记之间放置什么?

I have defined a style in my resource file like below

   <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>

I am at a loss as to what to put here within the data template

<ListBox x:Name="MyList" ItemsSource="{Binding }">
    <ListBox.ItemTemplate>
        <DataTemplate>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I tried using

<ContentPresenter Style="{StaticResource ListBoxStyle}"></ContentPresenter> 

and even

<ContentControl Style="{StaticResource ListBoxStyle}"></ContentControl>`

but got this error

Failed to assign to property 'System.Windows.FrameworkElement.Style'.

What do I put in between the DataTemplate tags if I want to provide a custom style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-14 21:58:21

尝试:

<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>

这应该可以解决您的问题。

如果定义样式,则可以定义 ListBox 的外观(背景、前景……)。
您可以在这里获取默认样式:http://msdn.microsoft.com/en-us/library/cc278062(v=vs.95).aspx

ItemTemplate(它是一个 DataTemplate)定义了如何呈现数据列表的单个元素看起来像(您使用绑定等等......)。

如果您想为单个元素定义样式,例如 MouseOver、Focused 等,您可以为 ListBoxItems 编写样式。您可以通过 ItemContainerStyle 将其添加到列表框。

<ListBox ItemContainerStyle="{StaticResource YourResourceKey}"/>

Try:

<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>

This shpuld solve your problem.

If you define a style, you define how the ListBox looks like (Background, Foreground, ...).
You can get the default style here:http://msdn.microsoft.com/en-us/library/cc278062(v=vs.95).aspx

The ItemTemplate (it is a DataTemplate) defines, how a the data presentation of a single element of the list looks like (You use bindings and so on...).

If you want to define a style for the single elements, like MouseOver, Focussed, ... you write a style for the ListBoxItems. You can add it to the list box via ItemContainerStyle.

<ListBox ItemContainerStyle="{StaticResource YourResourceKey}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文