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 7626189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:17:33+00:00 2026-05-31T05:17:33+00:00

I’ve been playing around with WPF for a while and I like how it

  • 0

I’ve been playing around with WPF for a while and I like how it is extremely versatile. I have read how to bind to a method with a single parameter (using a converter). However, purely hypothetically, is there a way to bind to a function that has access to local variables in the class?

For example, say I have a ListBox of TextBoxes to be bound to a List of Strings, defined and manipulated in the code behind, through a DataTemplate. Instead of displaying the entire string, however, each TextBox only displays the first N words of its corresponding string, which is easily done by a method that accepts the string and N as parameters. N, however, is free for the user to configure, and thus must be a local variable. For this to be a flexible control, N cannot be a static variable as we want it to be possible to have many windows with such ListBoxes with different Ns; besides, that would be extremely inelegant.

Now we can’t use a converter as converters only accept a single variable, while we need to access an N. We can’t cheat and use a static variable either.

We also cannot bind the TextBoxes to a local property that has access to local variables as we do to normal objects, as we are binding to a whole List of objects.

From what I see, ObjectDataProvider only allows you to call a method of the class of a given object, and not a method defined in the window. It is, I believe, a waste to create a new class and copy N to every instance of the class just for the sake of achieving such simple functionality.

Of course in a real scenario it’s possible to sidetrack this problem through such measures as creating an object including the string and N as in the previous paragraph, or including both the string and another string comprising the first two words of the string precomputed. I’m just asking whether such a functionality exists for WPF because it fits well into the concept WPF is based on and I believe that such a functionality would be useful some way or another.

I’m sorry if the solution is immediately obvious; I’ve been searching for hours.

If there is a solution, is there a way to do so completely in XAML, with only the method defined in the code behind?

EDIT: Thanks a lot for the suggestion, Clemens… N can’t be passed as ConvertParameter as it is not possible to bind to ConvertParameter. As for the first suggestion, I’m trying it out, but I’m getting an error Specified cast is not valid. at the Convert method of the IMultiValueConverter. Rather than N, values[1] is DependencyProperty.UnsetValue. Below is the code:

XAML

<MultiBinding>
    <MultiBinding.Converter>
        <my:StringShorternerConverter />
    </MultiBinding.Converter>
    <Binding Path="Title" />
    <Binding ElementName="me" Path="N" />
</MultiBinding>

Inside StringShorternerConverter

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return ShortenText((String)values[0], (Int32)values[1]);
}

EDIT 2: Looks like that error was due to some irrelevant mistake. Yeah, it works. Thanks a lot.

  • 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-05-31T05:17:34+00:00Added an answer on May 31, 2026 at 5:17 am

    You can easily bind to multiple properties by using a MultiBinding in conjunction with a converter that implements IMultiValueConverter.

    You could also pass your variable N to the parameter argument of the Convert method of a normal converter by means of the Binding.ConverterParameter property.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words

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.