0

I'm trying to fill a button with a path and get an argument out of range exception. I'm binding in my View to a property on a model. Code for the property is:

public PunishmentName Name { get; set; }
public Geometry IconData 
{
    get
    {
        string data = "";
        switch (Name) 
        {
            case PunishmentName.Freeze:
                data = "M 0 0 Q 10 10 20 0";                                               
                break;
        }

        Path path = XamlReader.Load("<Path " + "xmlns=" + "'" + "http://schemas.microsoft.com/winfx/2006/xaml/presentation" + "'" + "  Data= '" + data + " '/>") as Path;
        Geometry geometry = path.Data;
        return geometry;
    }
}

code for the button in the view

<Button Grid.Row="1" BorderBrush="Transparent" >
    <Viewbox>
        <Path Data="{Binding Punishment.IconData}"/>
    </Viewbox>
</Button>

I know this question: is already been asked here: Windows Phone 7: How to parse Bezier Path string like in XAML?

I tried putting the Path into a grid and a canvas as well with the same result.

But it does'nt get solved there...And i'me new here so i can't comment and don't feel comfortable asking a question as an answer. I'me quit stuck here. I can think of a work around but that would mean a lot more work and uglier code...

The exception:

{System.Windows.ApplicationUnhandledExceptionEventArgs}
    base: {System.Windows.ApplicationUnhandledExceptionEventArgs}
    ExceptionObject: {System.ArgumentException: Value does not fall within the expected range.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
   at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
   at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
   at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
   at System.Windows.Data.BindingExpression.SendDataToTarget()
   at System.Windows.Data.BindingExpression.SourceAcquired()
   at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e)
   at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnAncestorDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)
   at System.Windows.FrameworkElement.OnTreeParentUpdated(DependencyObject newParent, Boolean bIsNewParentAlive)
   at System.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent, IManagedPeer newParent, Boolean bIsNewParentAlive, Boolean keepReferenceToParent)
   at MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(IntPtr oldParentElement, IntPtr parentElement, IntPtr childElement, Byte bIsParentAlive, Byte bKeepReferenceToParent, Byte bCanCreateParent)}
    Handled: false

I've done exactly the same in a WPF application with Geometry.Parse but this still is'nt avalable for the phone...

Community
  • 1
  • 1
  • There is a build-in converter - so you don't need `IconData` to be a *Geometry* - make it `public string IconData` and in getter `return data` (for example data = "M 0 0 Q 10 10 20 0") - it should also work. – Romasz Sep 24 '14 at 20:10
  • Is this it? http://stackoverflow.com/questions/22119053/system-exception-system-argumentexception-value-does-not-fall-within-the-expec – dbc Sep 24 '14 at 21:05
  • thank you dbc and everybody, Romasz gave the solution. – David Bijttebier Sep 24 '14 at 21:28

2 Answers2

0

I think you may need to give the rest of the code to see where you're going wrong. I wrote this code for Windows Phone 8.1 WinRT and it worked fine:

<Grid>
    <Button Grid.Row="1" BorderBrush="Transparent" >
        <Viewbox>
            <Path Data="{Binding IconData}"/>
        </Viewbox>
    </Button>
</Grid>



public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;
        Punishment MyPunishment = new Punishment();
        MyPunishment.Name = Punishment.PunishmentName.Freeze;
        this.DataContext = MyPunishment;
    }
}

public class Punishment
{
    public enum PunishmentName { Freeze, Burn };
    public PunishmentName Name { get; set; }
    public Geometry IconData
    {
        get
        {
            string data = "";
            switch (Name)
            {
                case PunishmentName.Freeze:
                    data = "M 0 0 Q 10 10 20 0";
                    break;
            }
            Path path = XamlReader.Load("<Path " + "xmlns=" + "'" + "http://schemas.microsoft.com/winfx/2006/xaml/presentation" + "'" + "  Data= '" + data + " '/>") as Path;
            Geometry geometry = path.Data;
            return geometry;
        }
    }
}
Matt Small
  • 2,182
  • 1
  • 10
  • 16
  • Thanks, it is a bit difficult giving more code. What code do you mean exactly? I'me setting the datacontext of the viewmodel in code behind simmilar as you do. The easy way"this.DataContext = ..." strang btw it works fine with you. Thus off course it most be my code... – David Bijttebier Sep 24 '14 at 19:19
  • the only difference i see is i can't set the navigationcaching mode never did that either actually – David Bijttebier Sep 24 '14 at 19:31
0

Romasz gave the answer. It is not necessary to bind to Geometry. A string is perfectly fine... I tought it was necessary in WPF tough hence the confusion... thank you Romasz