0

I have a ScreenHeightConverter, and want to use it on the ItemHeight of a GridView.

But I don't know if I use it properly, because if I debug it, it doesn't even jump to the converter.

My Code:

public class ScreenHeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        double ScreenHeight = (int)Window.Current.Bounds.Height;
        double factor = 1050/(double)value;
        return (int)(ScreenHeight/factor);
    }
}

<VariableSizedWrapGrid Margin="3" ItemHeight="{Binding '80',Converter={StaticResource ScreenHeightConverter}}" ItemWidth="255"/>
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
user1572130
  • 65
  • 10

2 Answers2

2

Your Binding is not being specified in the correct way.

In the way you are currently specifying the Binding you are saying find a property called 80 (i.e. the Path) on the object currently set/inherited via the DataContext property and use its value.

(property names can't start with a digit and I think the quoted syntax to indicate the Path may be wrong anyhow...thus the Binding is wrong....and your converter doesn't get called).

To confirm this, you can look at the Output Window in Visual Studio while you're debugging the application...it should inform you about Bindings that have errors...see these links for details:


I believe your intention was to have a literal constant value which is passed to your converter in order to calculate a suitable value.

Instead of using a Converter, you should look at doing it using a MarkupExtension.....yes you could "fudge it" with a converter, by binding to an arbitrary object and just passing your 80 value as a ConverterParameter....but that's not the best way and a big kludge.

Here are some links on writing a MarkupExtension:

...so create a MarkupExtension derived class e.g. HeightAdjustedExtension : MarkupExtension .... implement the ProvideValue method and "properties" on the extension that can funnel incoming data.

...then you could use it in this way ...

ItemHeight="{myns:HeightAdjusted 80}"

This is not tested, but something like this (gives you something to play around with):

public class HeightAdjustedExtension : MarkupExtension
{
    [ConstructorArgument("height")]
    public string Height { get; set; }

    public HeightAdjustedExtension () { }

    public HeightAdjustedExtension (string height)
    {
        Height = height;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        int theheight;
        int.TryParse( Height, out theheight );

        double ScreenHeight = (int)Window.Current.Bounds.Height;
        double factor = 1050/(double)theheight ;
        return (int)(ScreenHeight/factor);        
    }
}
Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47
  • Yes thanks, you are Right. If i do it with the ConverterParameter it works, its indeed not the best way, but i dont know if MarkupExtension are an alternative. – user1572130 Jan 02 '13 at 13:38
  • I want to use this Convereter in many places, to fix the Height and Width for Tablets and other resulutions. – user1572130 Jan 02 '13 at 13:42
0

You need to create a resource for the converter.

First you need to Add namespace. Example

xmlns:converter="clr-namespace:SDKSample;assembly=SDKSampleLibrary"

Next you need to add resource to appropriate place (UserControl, Window, App or other object)

<UserControl.Resources> 
 <converter:ScreenHeightConverter x:key="screenHeightConverter"/>
</UserControl.Resources>

At last you can refer to static resource by key attribute value. "screenHeightConverter" in this case.

After that you can use the static resource reference in Binding

<VariableSizedWrapGrid Margin="3" ItemHeight="{Binding '80',Converter={StaticResource screenHeightConverter}}" ItemWidth="255"/>
Tilak
  • 30,108
  • 19
  • 83
  • 131