1

标题可能有点离题,所以我会尝试用每一个细节来解释它。我有一个正在处理的 WPF 应用程序。ListBoxItem我为( GenreStyle&& StationStyle)设置了自己的样式,为ItemContainerStyle.

这就是我的GenreStyle样子:

<DataTemplate x:Key="GenreStyle">
    <Grid Width="170" Height="170" Background="Transparent">
        <Image x:Name="GenreImage" Tag="{Binding Genre}" RenderOptions.BitmapScalingMode="HighQuality" Source="{Binding GenreSource}" Width="140" Height="140"/>
    </Grid>
</DataTemplate>

这就是我的StationStyle样子:

    <DataTemplate x:Key="StationStyle">
    <Grid x:Name="Gridder" Tag="{Binding SItem.StationName}" Width="125" Height="55">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>
        <Rectangle x:Name="Rectagler" Width="120" Height="50" RadiusX="5" RadiusY="5" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="2" Grid.RowSpan="2" StrokeThickness="1" Stroke="Black" Fill="#FFB20000"/>
        <TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" x:Name="StationName" Text="{Binding SItem.StationName}" Foreground="Black" FontSize="14" FontWeight="SemiBold" TextWrapping="NoWrap" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0"/>
        <TextBlock Grid.Column="0" Grid.Row="1" x:Name="StationCountry" Text="{Binding SItem.StationCountry}" Foreground="WhiteSmoke" FontSize="11" FontWeight="Light" TextWrapping="NoWrap" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,8"/>
        <Image Tag="{Binding SItem.StationName}" Width="15" Height="15" RenderOptions.BitmapScalingMode="HighQuality" Grid.Column="1" Grid.Row="1" Source="{Binding FavSource}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,10,10"/>
    </Grid>
</DataTemplate>

这是我的ItemContainerStyle

    <Style x:Key="ContainerStyle" TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Grid Background="Transparent">
                    <ContentPresenter
                      x:Name="contentPresenter"
                      Content="{TemplateBinding Content}"
                      ContentTemplate="{TemplateBinding ContentTemplate}"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                      Margin="{TemplateBinding Padding}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="ListBoxItem.Template" Value="{StaticResource GenreStyle}">
            <Setter Property="Tag" Value="{Binding Genre}"/>
        </Trigger>
        <Trigger Property="ListBoxItem.Template" Value="{StaticResource StationStyle}">
            <Setter Property="Tag" Value="{Binding Sitem.StationName}"/>
        </Trigger>
    </Style.Triggers>
</Style>

正如您ItemContainerStylei中看到的那样,我Trigger为.and 设置了一个ListBoxItem.Template。如果ListBoxItem.Template是,GenreStyle我想从不同的值绑定属性。但是在我的应用程序加载之前,这会Tag在我的 main 中失败并出现异常。XAML

ListBox这样设置:

<ListBox x:Name="StationsListLB" ItemTemplate="{DynamicResource GenreStyle}" ItemContainerStyle="{DynamicResource ContainerStyle}">

注意ListBoxItem.Template——我多次更改我的 C# 代码。

我得到的错误是这样的:

System.Windows.DataTemplate' is not a valid value for property 'Template'.

我如何设置我的触发器ItemContainerStyle以更改绑定?

提前致谢。

4

0 回答 0