简体   繁体   中英

XAML Bind control property to assigned style

What I want to achieve is, to create a style for a TextBlock Control which scales in height on mouseover depending on the amount of items which are being hold by the TextBlock.
My approach is to take the item amount and pass it to a converter which gives returns the TextBlock.Height * items.count but I'm having troubles achieving this. Also I want this style to only trigger for one column of my ListView and not for all but I haven't thought about how to do that yet.
Currently my XAML code looks like this:

<ListView ItemsSource="{Binding SwItemToPopulate}" Name="lvSoftware" DockPanel.Dock="Top" Margin="10,10,10,10" MinHeight="325" Height="Auto" Width="Auto">
<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Height">
                    <Setter.Value>
                        <Binding 
                            Path="Dependencies.Count"
                            Converter="{StaticResource ItemCountToTextBoxHeightConverter}"
                            ConverterParameter="TextBlock.Height" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>
<ListView.View>
    <GridView>
        <GridViewColumn Header="Dependencies">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Dependencies}"/>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>

and this is my converter:

namespace test.Converter
{
class ItemCountToTextBoxHeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (targetType != typeof(double))
            throw new InvalidOperationException("The target must be an Integer"); 

        int tbHeight = (int)parameter;
        return (Double)value * tbHeight;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

}
}

If I set a Brakepoint to the Converter I can see that the value parameter is 0 and the parameter is TextBlock.Height is this because my binding is wrong or is this not even possible what I'm trying to do?

Edit I'm using a Tooltip now to achieve this. Thank all for the great comments and answers!

TextBlock isn't designed to handle this scenario. You should use ItemsControl instead.

Changing the GridViewColumn.CellTemplate to a ItemsControl would achieve this.

Having said that, try using TextBlock.ActualHeight and not Height .

You could reverse the property trigger so that it instead resets the item to the normal height when the mouse isn't over and therefore textblock height can be bound to Dependency.Count. I have hardcoded the normal heights as 20 because I wasn't sure how else to do it.

<ListView ItemsSource="{Binding Items}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="False">
                    <Setter Property="Height" Value="20"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Dependencies">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Dependencies}" 
                                    Height="{Binding Path=Dependencies.Count, Converter={StaticResource ItemCountToTextBoxHeightConverter}, ConverterParameter=20}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM