简体   繁体   English

样式不适用于ListView中的StackPanel项目-Windows Universal

[英]Style doesn't apply to StackPanel items in ListView - Windows Universal

I have created a listview with databinding and a "Itemstemplate" which takes a "Datatemplate" where I have a Stackpannel but the style doesn't apply to the stackpannel, there is no space between the textblocks in the stackpannel: 我创建了一个带有数据绑定的列表视图和一个“ Itemstemplate”,其中带有一个“ Datatemplate”,其中我有一个Stackpannel,但是样式不适用于Stackpannel,stackpannel中的文本块之间没有空格:

 <ListView Grid.Row="1" DataContext="{Binding Source={StaticResource ViewModel}}" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Visible">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Background="Gray" >
                    <StackPanel.Resources>
                        <Style TargetType="TextBlock" x:Key="margintextblock">
                            <Setter Property="Margin" Value="10,0,0,0"/>
                        </Style>
                    </StackPanel.Resources>
                    <TextBlock Style="{StaticResource listviewtextblock}" Text="{Binding Path=Firstname}" Foreground="Gold"></TextBlock>
                    <TextBlock Style="{StaticResource listviewtextblock}" Text="{Binding Path=Lastname}" Foreground="Black"></TextBlock>
                    <TextBlock Style="{StaticResource listviewtextblock}" Text="{Binding Path=Id}" Foreground="OrangeRed"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

So what's wrong? 那怎么了

You have to remove x:Key="margintextblock" because of this the style doesn't apply automatically to the TextBlocks 您必须删除x:Key="margintextblock"因为该样式不会自动应用于TextBlocks

By setting the x:Key property on a style, you are telling WPF that you only want to use this style when you explicitly reference it on a specific control. 通过在样式上设置x:Key属性,可以告诉WPF仅当在特定控件上显式引用该样式时才要使用此样式。

Take a look on this tutorial 看一下本教程

EDITED EDITED

And you also have another problem - you are setting style for you TextBlocks Style="{StaticResource listviewtextblock}" 而且您还有另一个问题-您正在为自己设置样式TextBlocks Style="{StaticResource listviewtextblock}"

In this case what you have to do is inherit StackPanel TextBlock style from listviewtextblock style 在这种情况下,您需要从listviewtextblock样式继承StackPanel TextBlock样式

<StackPanel.Resources>
    <Style TargetType="TextBlock" BasedOn="{StaticResource listviewtextblock}">
         <Setter Property="Margin" Value="10,0,0,0"/>
     </Style>
 </StackPanel.Resources>

And remove style Style="{StaticResource listviewtextblock}" from TextBlocks 然后从TextBlocks删除样式Style="{StaticResource listviewtextblock}"

you code should looks like this 您的代码应如下所示

 <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Background="Gray" >
                    <StackPanel.Resources>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource listviewtextblock}" >
                            <Setter Property="Margin" Value="10,0,0,0"/>
                        </Style>
                    </StackPanel.Resources>
                    <TextBlock Text="{Binding Path=Firstname}" Foreground="Gold"></TextBlock>
                    <TextBlock Text="{Binding Path=Lastname}" Foreground="Black"></TextBlock>
                    <TextBlock Text="{Binding Path=Id}" Foreground="OrangeRed"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM