只要ListView没有超过五个项目,显示按钮

时间:2014-05-01 12:28:09

标签: wpf listview listviewitem datatrigger

我有一些ListViews,每个人可以包含最多5 ListViewItems。只要少于5个项目,就应该显示Button。我已经编写了Value Converter,如果Count小于5,则返回true。但是现在我有问题造型ListView。因为即如果有三个ListViewItems,它们应该用正常样式设置样式,而“4”项应该是Button。我的意思是我在ListViewItem内添加了一个新的DataTrigger。这有可能还是有更好的方法来实现这一目标?

Style.xaml

<Style TargetType="{x:Type ListView}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <UniformGrid Rows="5" Columns="1"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>      
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count, Converter={StaticResource ItemCounterConverter}}" Value="true">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBlock>No items to display</TextBlock>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>
<!--ListViewItem-->
<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">                                    
                <Border BorderBrush="#5076A7" BorderThickness="1">
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <GradientStop Color="#FFFFFF" Offset="0.0"/>
                            <GradientStop Color="#C0D3EA" Offset="1.0"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <StackPanel TextElement.FontFamily="Segoe UI" TextElement.FontSize="12">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="15"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TextBlock Padding="3,0,0,0" Text="{Binding Betreff}" TextTrimming="CharacterEllipsis" Grid.Column="0" Grid.Row="0"/>
                            <Button FontSize="7" Content="X" Grid.Column="1" Grid.Row="0"/>
                        </Grid>
                        <TextBlock Padding="3,0,0,0" Text="{Binding Kunde}"/>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontWeight="Bold" Padding="3,0,0,0" Text="{Binding Ort}"/>
                            <TextBlock Padding="3,0,0,0" Text="("/>
                            <TextBlock Text="{Binding Alternative}"/>
                            <TextBlock Text=")"/>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

更新
谢谢这看起来很有希望,你明白了。但我在实施它时遇到了问题。每个ListView都有不同的ItemSource(它们都是ObservableCollections),所以我不知道是否可以在ItemSource内设置Style?但是最后的按钮应该对所有人都可用。我的StylesListViews本身位于不同的文件中。我ListViews的定义如下:

AppointmentOverview.xaml

<ListView Grid.Column="1" Grid.Row="3" ItemsSource="{Binding Mov}"/>
    <ListView Grid.Column="1" Grid.Row="4" ItemsSource="{Binding Mon}"/>
    <ListView Grid.Column="2" Grid.Row="3" ItemsSource="{Binding Div}"/>
    <ListView Grid.Column="2" Grid.Row="4" ItemsSource="{Binding Div}"/>
    <ListView Grid.Column="3" Grid.Row="3" ItemsSource="{Binding Miv}"/>
    <ListView Grid.Column="3" Grid.Row="4" ItemsSource="{Binding Min}"/>
    <ListView Grid.Column="4" Grid.Row="3" ItemsSource="{Binding Dov}"/>
    <ListView Grid.Column="4" Grid.Row="4" ItemsSource="{Binding Don}"/>
    <ListView Grid.Column="5" Grid.Row="3" ItemsSource="{Binding Frv}"/>
    <ListView Grid.Column="5" Grid.Row="4" ItemsSource="{Binding Frn}"/>

0 个答案:

没有答案