3

The whole “story” only to be able to configure everything in XAML. I’m trying to pass parameter (variable from my Application class) to ObjectDataProvider(ODP) method. The idea was to use markup extension. Now at runtime everything works perfect and extension returns this parameter for ODP. At design time it didn’t work. Are markup extensions not usable at design time? Are there any way to check if they are called due design time?

Extension:

namespace ZApplication
{
    public class ZAppExtension : MarkupExtension
    {
        public ZAppExtension() { }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            MessageBox.Show("ProvideValue called");
            return @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=TermineDB;Integrated Security=True;Persist Security Info=True";
        }
    }
}

XAML ObjectDataProvider:

   <ObjectDataProvider x:Key="objKalender"
                        MethodName="GetKalender"
                        ObjectType="{x:Type zzzDataLayer:TermineAPI}">

        <ObjectDataProvider.MethodParameters>
            <z:ZAppExtension></z:ZAppExtension>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

Binding that didnt work at design time:

<ListBox x:Name="listBox" Height="129" Margin="19,0,18,37" VerticalAlignment="Bottom"  
         ItemsSource="{Binding Mode=OneWay, Source={StaticResource objKalender}}" 
         DisplayMemberPath="Beschreibung"  SelectedValuePath="Id" 
         SelectedIndex="2"  SelectedValue="{Binding PrimaryCalendarId}" BorderThickness="1"/>

Proove that Extension works:

   <TextBlock x:Name="textBlock1" HorizontalAlignment="Right" Margin="0,210,42,190" TextWrapping="Wrap" Text="{z:ZApp}" Width="247"/>

If markups are disallowed/somehow disabled at design time what will be a way to pass any variable to MethodParameter?

Any help will be appreciated.

edit for adding GetKalender:

public static IEnumerable<Kalender> GetKalender(String strConnectionString=null)
{
    zEntityContext.zDataContext dc;
    if (String.IsNullOrEmpty(strConnectionString))
        dc = new zEntityContext.zDataContext();
    else
        dc = new zEntityContext.zDataContext(strConnectionString);

    return dc.Kalendere.Select(kalender => new Kalender(kalender, dc));
}

edited for changing DinamicResource to StaticResource in binding of ListBox.

AndrewZ
  • 31
  • 5

1 Answers1

0

How to check if your MarkupExtension is used in Designer mode ?

 public override object ProvideValue(IServiceProvider serviceProvider)
    {
       IProvideValueTarget ipvt = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
       if (ipvt.TargetObject is DependencyObject && System.ComponentModel.DesignerProperties.GetIsInDesignMode((DependencyObject)ipvt.TargetObject) == true)
                return "No designer mode please !";

        MessageBox.Show("ProvideValue called while running !");
        return @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=TermineDB;Integrated Security=True;Persist Security Info=True";
    }

How to overcome use of MarkupExtension as parameter to a method ?

In the approach above, return something else which might be String.Empty or null, while in designer mode. And then check the value of strConnectionString in the called method and simply return null. ItemsSource will get null and no exception will be thrown.

Proposed change in your GetKalender method :

if (String.IsNullOrEmpty(strConnectionString))
        return null;
Val
  • 21,938
  • 10
  • 68
  • 86
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • Thank you. Now I know how to retrieve desgn-status being in non UI element. Up to main issue - still I cant understand why this binding didnt work at design time. PS: Edited topic starter - added GetKalender. – AndrewZ Jul 24 '16 at 08:42
  • Are there other conventional methods ( without extension) that allow me to pass static property from some object as paramater to ObjectDataProvider? I like how it could be done with extension and possibilities extension bought to table - but only if this works reliably:) – AndrewZ Jul 24 '16 at 08:45
  • Anjum, thank you for proposition. But exeption want my problem - problem was extension wasnt called at design time. Overcoming by checking everywhere if somthing in design mode is bad solution ( for me atleast). At this moment i need to produce clean XAML and allow to see some data in design time. This extension (and parameter for ODP) was meant to show different databases connections based on my configuration. This has nothing to do with App.config and consorts. Anyways, its strange that this extension didnt called at designtime but rather at runtime. – AndrewZ Jul 24 '16 at 10:42
  • If I dont use extension ( and parameter in GetKalenders) everything works fine. But for sake of XAML based design i have to. – AndrewZ Jul 24 '16 at 10:42
  • Not everything will run during design time. Extension code is running in design time, why ObjectDataProvider method will run in design time ? – AnjumSKhan Jul 24 '16 at 12:03