WPF基于ListBoxItem模板触发绑定

时间:2013-08-14 16:30:05

标签: c# wpf binding triggers

标题可能有点偏离,所以我会尝试用每个细节来表达它。我有一个WPF应用程序,我正在努力。我为ListBoxItemGenreStyle& 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>

正如您在ItemContainerStyle中看到的我为Trigger设置了ListBoxItem.Template。如果ListBoxItem.TemplateGenreStyle我想从另一个值中取出Tag属性。但是在我的主XAML加载应用程序之前,这会失败并出现异常。

我将ListBox设置为:

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

注意 - 我多次从C#代码更改ListBoxItem.Template

我得到的错误是:

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

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

提前致谢。

0 个答案:

没有答案