0

我有一个列表视图:

<ListView Name="SelectedFeeds">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}" 
               BasedOn="{StaticResource {x:Type ListViewItem}}">
            <Style.Triggers>
                 <Trigger Property="IsSelected" Value="True">

                 </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" Name="panel">
                <TextBlock x:Name="Title" FontSize="24" 
                           Text="{Binding Title, IsAsync=True}" TextWrapping="Wrap" />
                        <Label x:Name="PubDate" FontSize="10" 
                               Content="{Binding Path=PubDate, IsAsync=True}" />
                        <TextBlock my:HtmlParser.HTMLText=
                                   "{Binding Path=Description, IsAsync=True}" 
                                   TextWrapping="Wrap" 
                                   Width="{Binding ElementName=panel, 
                                   Path=ActualWidth}" Height="0" />
             </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

DataTemplate 中有一个高度较大的 TextBlock。

At start I'm setting height of this textblock to "0" and when ListViewItem is selected, I need to set that height to "Auto".

这可能可以通过触发器来完成,但我无法弄清楚。

4

2 回答 2

4

对于触发方法,请在 DataTemplate 中使用它。

<ListView Name="SelectedFeeds">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" Name="panel">
                <TextBlock x:Name="Title" FontSize="24" 
                           Text="{Binding Title, IsAsync=True}" TextWrapping="Wrap" />
                        <Label x:Name="PubDate" FontSize="10" 
                               Content="{Binding Path=PubDate, IsAsync=True}" />
                        <TextBlock x:Name="TextHolder"
                                   my:HtmlParser.HTMLText=
                                   "{Binding Path=Description, IsAsync=True}" 
                                   TextWrapping="Wrap" 
                                   Width="{Binding ElementName=panel, 
                                   Path=ActualWidth}" Height="0" />
             </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="True">
                    <Setter TargetName="TextHolder" Property="Height" Value="123"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

注意 上的x:Name属性TextBlock

于 2012-05-20T07:08:39.917 回答
1

您可以使用触发器来执行此操作(尽管我目前不知道如何),但您可以使用转换器来执行此操作。

public class BoolToLengthConverter : IValueConverter
{
    public BoolToLengthConverter()
    {
        TrueValue = double.NaN;
        FalseValue = 0;
    }

    [TypeConverter(typeof(LengthConverter))]
    public double TrueValue { get; set; }

    [TypeConverter(typeof(LengthConverter))]
    public double FalseValue { get; set; }

    #region Implementation of IValueConverter

    public object Convert(object value, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        return System.Convert.ToBoolean(value) ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, 
                              object parameter, CultureInfo culture)
    {
        return TrueValue.Equals(value);
    }

    #endregion
}

Xaml

<ListView.Resources>
    <Converters:BoolToLengthConverter 
         x:Key="BoolToHeightConverter" TrueValue="Auto" FalseValue="0" />        
</ListView.Resources>

<TextBlock ... Height="{Binding Path=IsSelected, 
               Converter={StaticResource BoolToHeightConverter}, 
               RelativeSource={RelativeSource Mode=FindAncestor, 
               AncestorType={x:Type ListViewItem}}}"/>

您当然可以通过使用 BoolToVisibilityConverter 并绑定到 TextBlock 的 Visibility 属性来获得相同的结果。

于 2012-05-19T19:07:47.003 回答