Reputation: 31
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.
Upvotes: 3
Views: 797
Reputation: 9827
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;
Upvotes: 0