Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9160281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:42:05+00:00 2026-06-17T13:42:05+00:00

Currently I have markup like this <TextBlock> <TextBlock.Text> <Binding Path=Value ElementName=window ConverterParameter=M> <Binding.Converter> <local:DatePartValueConverter

  • 0

Currently I have markup like this

<TextBlock>
  <TextBlock.Text>
    <Binding Path="Value" ElementName="window" ConverterParameter="M">
      <Binding.Converter>
        <local:DatePartValueConverter />
      </Binding.Converter>
     </Binding>
  </TextBlock.Text>
</TextBlock>

I’d like to shorten it to something like this

<TextBlock Text="{Binding Path=Value,ElementName=window,
  ConverterParameter=M,Converter={local:DatePartValueConverter}}" />

But the compiler barfs because DatePartValueConverter isn’t derived from MarkupExtension. Is there no other way to get the short form markup to create an instance of DatePartValueConverter?

Incidentally I tried deriving from MarkupExtension and it does solve the problem. My implementation of ProvideValue looked like this

public override object ProvideValue(IServiceProvider serviceProvider)
{
  return new DatePartValueConverter();
}

and it works, but I remain hazy on the origin and nature of serviceProvider and what one might be expected to do with it.

Interestingly, when I used the Visual Studio 2012 binding editor on a binding that used this markup extension it promptly expanded it again, making the whole markup extension support thing a bit pointless.

I should probably mention that I need a private instance for each binding because it maintains internal state – it needs to know the whole DateTime value to set some aspect, like this

public object ConvertBack(object value, Type targetType, 
  object parameter, System.Globalization.CultureInfo culture)
{
  culture = System.Globalization.CultureInfo.CurrentCulture;
  var strValue = value as string;
  int y = _value.Year, M = _value.Month, d = _value.Day,
    H = _value.Hour, m = _value.Minute, s = _value.Second;
  if (strValue == null)
    return null;
  else
  {
    string p = parameter as string;
    switch (p)
    {
      case "yyyy":
        y = int.Parse(strValue); break;
      case "yy":
        y = (strValue.Length == 4) ?
          int.Parse(strValue) :
          int.Parse(DateTime.Now.Year.ToString().Substring(0, 2) + strValue);
        break;
      case "M":
      case "MM":
        M = int.Parse(strValue); break;
      ...
    }
  }
  return new DateTime(y, M, d, H, m, s);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:42:06+00:00Added an answer on June 17, 2026 at 1:42 pm

    You can create an instance of DatePartValueConverter as a resource, then use it.

    <Window.Resources>
      <local:DatePartValueConverter x:Key="datePartValueConverter" />
    </Window.Resources>
    
    <TextBlock Text="{Binding Path=Value, ElementName=window,
      ConverterParameter=M, Converter={StaticResource datePartValueConverter}}" />
    

    EDIT If you did want to make the converter a MarkupExtension then you can do so. You can either return a new instance in ProvideValue or you can return the current instance with return this;

    By returning the current instance you can have properties in your converter, and allows you to do things like this.

    public class DatePartValueConverter : MarkupExtension, IValueConverter {
        public string ParseType { get; set; }
        // other methods
    }
    
    <TextBlock Text="{Binding Path=Value, ElementName=window,
      Converter={local:DatePartValueConverter ParseType=M}}" />
    

    As for IServiceProvider see MarkupExtension.ProvideValue — Is the IServiceProvider actually used?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing markup extension. I have XAML like this <TextBlock Text={ui:Test SomeInfo} /> and
I have markup like <div> <label for=a class=float-left>column 1 row 1</label> <input type=text value=column
Currently have password protection on my main and sub directories, however I'd like to
I currently have a MySQL table like: id | friend 1 | 2 1
I currently have an ordered list that I want to markup using the new
On a webpage, I have markup like the following: <h3>Title Goes here</h3> <a href=...>Link
I have markup in my page that includes the tag: <body> <object codebase=blah codetype=text/blah>
I currently have page-specific javascript files that look something like: $(document).ready(function () { namespace.home.list
I'm using this jQuery fullscreen plugin, and I'd like to have it so that
I basically have this markup coming from my JSP. I add class in each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.