Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Binding to Converter
Message
De
08/07/2013 03:09:20
 
 
À
28/06/2013 20:13:47
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01577175
Message ID:
01577937
Vues:
35
Hi,
I PM'd you on this. Did you see it ?

What you have to bear in mind is that if the View is opened in the IDE then the code is actually running and some objects or connections which would be available at runtime may not be instantiated or available.

If you can't get round the problem any other way you could simply bypass the Convert method when in design mode (see http://msdn.microsoft.com/en-us/library/system.componentmodel.designerproperties.getisindesignmode.aspx )

Also, as a matter of principle, the logic in a Convert/ConvertBack method should be as light as possible - if you can simplify it you'll get better performance.



>Bump
>
>Here's the code in my converter. Maybe I am missing a null check somewhere that someone else can see?
>
>
using System;
>using System.Collections.Generic;
>using System.Linq;
>using System.Text;
>using System.Windows.Data;
>using System.Windows;
>using SamaanSystems.IBC.Business;
>
>namespace SamaanSystems.IBC.WPF
>{
>    public class Inv_PkToReceiptNumberConverter : IValueConverter
>    {
>        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
>        {
>
>            if (value == null || value.GetType() != typeof(Guid))
>                return DependencyProperty.UnsetValue;
>
>            Receipt receipt = new Receipt();
>            ReceiptEntity re = receipt.GetReceiptByInvoicePK((Guid)value);
>
>            if (re != null && re.rct_pk != Guid.Empty)
>            {
>                return re.rct_number;
>            }
>            else
>            {
>                CashReceipt cashReceipt = new CashReceipt();
>                CashReceiptEntity cre = cashReceipt.GetCashReceiptByInvoicePK((Guid)value);
>
>                if (cre != null && cre.crt_pk != Guid.Empty)
>                {
>                    return cre.crt_number;
>                }
>                else
>                {
>                    return DependencyProperty.UnsetValue;
>                }
>
>            }
>        }
>
>        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
>        {
>            if (value == null || value.GetType() != typeof(Guid))
>                return Guid.Empty; 
>            
>            Receipt receipt = new Receipt();
>            ReceiptEntity re = receipt.GetReceiptByNumber(System.Convert.ToInt32(value));
>
>            if (re != null)
>            {
>                return re.rct_pk;
>            }
>            else
>            {
>                CashReceipt cashReceipt = new CashReceipt();
>                CashReceiptEntity cre = cashReceipt.GetCashReceiptByNumber(System.Convert.ToInt32(value));
>
>                if (cre != null)
>                {
>                    return cre.crt_pk;
>                }
>                else
>                {
>                    return Guid.Empty;
>                }
>            }
>        }
>    }
>}
>
>
>
>>Just bumping this in case everybody missed it. It's still not completely solved. :(
>>
>>>Update
>>>
>>>I solved this. I had not completed the ConvertBack of the converter. When I added that code in and rebuilt the solution it all worked as desired EXCEPT that the designer gives me "An Unhandled Exception has occurred". This is the detail of the error:
>>>
>>>System.Reflection.TargetInvocationException
>>>Exception has been thrown by the target of an invocation.
>>> at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
>>> at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
>>> at System.Delegate.DynamicInvokeImpl(Object[] args)
>>> at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
>>> at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
>>>
>>>
>>>System.NullReferenceException
>>>Object reference not set to an instance of an object.
>>> at OakLeaf.MM.Main.mmAppBase.get_DatabaseMgr()
>>> at OakLeaf.MM.Main.Business.mmBusinessObject.GetDataAccessObject(String databaseKey, Boolean localDataAccessObject)
>>> at OakLeaf.MM.Main.Business.mmBusinessObject.GetDataAccessObject(String databaseKey)
>>> at OakLeaf.MM.Main.Business.mmBusinessObject.GetDataAccessObject()
>>> at OakLeaf.MM.Main.Business.mmBusinessObject.CreateParameter(String name, Object value)
>>> at SamaanSystems.IBC.Business.Receipt.GetReceiptByInvoicePK(Guid invoicePK) in D:\Development\VS2010\IBC\IBC Business Objects\Receipt\Receipt.Partial.cs:line 39
>>> at SamaanSystems.IBC.WPF.Inv_PkToReceiptNumberConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture) in D:\Development\VS2010\IBC\IBC WPF\Converters\Inv_PkToReceiptNumberConverter.cs:line 21
>>> at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
>>> at System.Windows.Data.BindingExpression.Activate(Object item)
>>> at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
>>> at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
>>> at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
>>> at MS.Internal.Data.DataBindEngine.Run(Object arg)
>>> at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
>>> at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
>>> at System.Windows.ContextLayoutManager.UpdateLayout()
>>> at System.Windows.UIElement.UpdateLayout()
>>> at System.Windows.Interop.HwndSource.SetLayoutSize()
>>> at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
>>> at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
>>> at MS.Internal.DeferredHwndSource.ProcessQueue(Object sender, EventArgs e)
>>>
>>>Anyone have a clue what this is trying to tell me?
>>>
>>>end of update
>>>
>>>Hi,
>>>
>>>I am trying to bind a textbox to a converter. This is to display the receipt number that the currently displayed invoice is on. So I set my XAML up like this:
>>>
>>>
>>><Window.Resources>
>>>        <local:Inv_PkToReceiptNumberConverter x:Key="Inv_PkToReceiptNumberConverter"/>
>>></Window.Resources>
>>>
>>><TextBox Text="{Binding inv_pk, Converter={StaticResource Inv_PkToReceiptNumberConverter}, Mode=Default}" />
>>>
>>>But this gives me an error:
>>>
>>>"Object reference not set to an instance of an object"
>>>
>>>How do I get this to work?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform