views:

174

answers:

1

I'm looking for a simple, idiomatic method of replicating Excel's visual cue that a number is too large to be displayed in a column. I've got the following xaml:

<ListView.View>
    <GridView>
        <GridViewColumn ... />
    </GridView>
<ListView.View>

and what I'd like is if the text in the column is too small to be displayed (i.e. it's clipped), I want to replace the displayed data with '#' characters.

+1  A: 

You could multi-bind the cell's content to the actual number, to the width of the containing column, and to the desired size of the TextBlock. Then use a converter to convert select the content accordingly. Pseduo-XAML:

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <TextBlock x:Name="_textBlock">
            <TextBlock.Content>
                <MultiBinding Converter="{StaticResource MyConverter}">
                    <Binding Path="."/>
                    <Binding Path="ActualWidth" RelativeSource="{RelativeSource Ancestor, AncestorType={GridViewColumn}}"/>
                    <Binding Path="DesiredSize.Width" ElementName="_textBlock"/>
                </MultiBinding>
            </TextBlock.Content>
        </TextBlock>
    </Datatemplate>
</GridViewColumn.CellTemplate>

Pseduo-code:

public class MyConverter : IMultiValueConverter
{
    public object Convert(...)
    {
        object content = values[0];
        double actualWidth = (double)values[1];
        double desiredWidth = (double)values[2];

        if (desiredWidth > actualWidth)
        {
            return "######";
        }

        return content;
    }
}

Other than that, you could write your own TextBlock subclass that does a similar thing automatically, and then use that inside each column template.

HTH, Kent

Kent Boogaart