WPF列表框触发空更改默认样式

时间:2017-12-19 12:01:26

标签: c# wpf listbox

我有一部分代码可以使我的应用程序中的每个列表框在空时显示一条消息。

<Style TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
    <Style.Triggers>
        <Trigger Property="HasItems" Value="False">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBlock FontSize="20" Foreground="{DynamicResource ValidationErrorBrush}"
                                   TextAlignment="Center" Text="Geen data beschikbaar" Width="Auto" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

当我启用(推荐)启用它的应用程序时,空列表框确实会显示消息。

但是当它们被填充时,项目模板比它应该更宽。

这是我使用列表框的示例:

<ListBox x:Name="DataGridTraffic"
             Grid.Row="1"
             SnapsToDevicePixels="True"
             ItemsSource="{Binding Path=TrafficJams}"
             ScrollViewer.VerticalScrollBarVisibility="Hidden"
             SelectedIndex="-1"
             ItemTemplate="{DynamicResource TrafficJamDataTemplateShort}"
             AlternationCount="2"
             BorderThickness="0"
             HorizontalContentAlignment="Stretch"
             ItemContainerStyle="{DynamicResource MainListBoxStyle}" />

有没有人知道发生了什么?

编辑:

Here is it to wide

This is how it supposed to be

第一张图显示了启用触发器的方式。

第二个显示它应该是这样的方式。

修改

以下是其中一些要求。 这是与图片相同的Datatemplate。

<DataTemplate x:Key="BusDeparturesDataTemplateShort" DataType="{x:Type model:BusDeparture}">
    <Grid>
        <Grid.Style>
            <Style TargetType="{x:Type Grid}">
                <Setter Property="Background" Value="{DynamicResource ValidationErrorLighterBrush}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=DelayText}" Value="">
                        <Setter Property="Background" Value="Transparent" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
        <Grid Margin="4,8,4,4">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock TextAlignment="Center"
                       FontWeight="Bold"
                       FontSize="32"
                       Text="{Binding Path=DeparturePlatform}"
                       Grid.Column="0" Margin="0,0,5,0"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Center">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=PlatformChanged}" Value="True">
                                <Setter Property="Foreground" Value="{DynamicResource ValidationErrorBrush}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=PlatformChanged}" Value="False">
                                <Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidBrush}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid>
                    <StackPanel Orientation="Horizontal"
                                TextBlock.FontSize="18"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Center">
                        <TextBlock Text="{Binding Path=ServiceNumber}" Width="45" />
                        <TextBlock Text="{Binding Path=Destination}" />
                    </StackPanel>
                </Grid>
                <Grid Grid.Row="1">
                    <StackPanel Orientation="Horizontal"
                                TextBlock.FontSize="16"
                                HorizontalAlignment="Left">
                        <TextBlock Text="{Binding Path=DepartureTime, StringFormat=HH:mm}" Margin="0,0,4,0" />
                        <TextBlock Text="{Binding Path=DelayText}" TextAlignment="Right"
                                   Foreground="{DynamicResource ValidationErrorBrush}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal"
                                TextBlock.FontSize="16"
                                HorizontalAlignment="Right">
                        <TextBlock Text="{Binding Path=Carrier}" TextAlignment="Right"
                                   HorizontalAlignment="Stretch" />
                    </StackPanel>
                </Grid>
            </Grid>
        </Grid>
    </Grid>
</DataTemplate>

列表框所在的部分:

<Grid x:Name="MainGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <materialDesign:Card Margin="8" Grid.Column="0" Grid.Row="0" VerticalAlignment="Stretch"
                         materialDesign:ShadowAssist.ShadowDepth="Depth2" x:Name="CardTrain">
        ...
    </materialDesign:Card>
    <materialDesign:Card Margin="8" Grid.Row="0" Grid.Column="1" VerticalAlignment="Stretch"
                         materialDesign:ShadowAssist.ShadowDepth="Depth2" x:Name="CardBus">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Border BorderBrush="{DynamicResource MaterialDesignDivider}" BorderThickness="0,0,0,1">
                <TextBlock Text="Bustijden" Foreground="{DynamicResource PrimaryHueDarkBrush}"
                           TextAlignment="Center"
                           FontSize="36" FontWeight="ExtraBold" />
            </Border>
            <ListBox x:Name="DataGridBus"
                     Grid.Row="1"
                     SnapsToDevicePixels="True"
                     ItemsSource="{Binding Path=BusDepartures}"
                     ScrollViewer.VerticalScrollBarVisibility="Hidden"
                     SelectedIndex="-1"
                     ItemTemplate="{DynamicResource BusDeparturesDataTemplateShort}"
                     AlternationCount="2"
                     BorderThickness="0"
                     HorizontalContentAlignment="Stretch"
                     ItemContainerStyle="{DynamicResource MainListBoxStyle}"
                     VerticalAlignment="Top" />
        </Grid>
    </materialDesign:Card>
    <materialDesign:Card Margin="8" Grid.Row="0" Grid.Column="2" VerticalAlignment="Stretch"
                         materialDesign:ShadowAssist.ShadowDepth="Depth2" x:Name="CardTraffic">
        ...
    </materialDesign:Card>
    <materialDesign:Card Margin="8" Grid.Row="0" Grid.Column="3" VerticalAlignment="Stretch"
                         materialDesign:ShadowAssist.ShadowDepth="Depth2" x:Name="CardTwitter">
        ...
    </materialDesign:Card>
    <materialDesign:Card Margin="8" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4"
                         VerticalAlignment="Stretch" materialDesign:ShadowAssist.ShadowDepth="Depth2"
                         x:Name="CardNews">
        ...
    </materialDesign:Card>
</Grid>

0 个答案:

没有答案