سؤال

Suppose I have property

FIrstName and LastName

I need to bound it with single textbox.

So in just single textbox I can display both FirstName and LastName.

Then how could it be possible?

هل كانت مفيدة؟

المحلول

You can use multibinding to do that
For OnWay Binding use this:

<TextBox>
    <TextBox.Text>
        <MultiBinding StringFormat="{}{0} {1}" Mode="OneWay">
            <Binding Path="FirstName"/>
            <Binding Path="LastName"/>
        </MultiBinding>
    </TextBox.Text>
</TextBox>

For more information about MutiBinding Class look at here

نصائح أخرى

Try the MultiBinding Class :

<TextBlock>
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource myConverter}">
      <Binding Path="FirstName" />
      <Binding Path="LastName" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

A third property FullName, perhaps.

For better help, please ask a better question (what are your types, e.g.).

edit: Anyways, I'd recommend to have at least two textboxes, so you can safely handle "multipart names", like the above mentioned Karl Heinz Schmidt-Meyer von Neuenhausen zu Bad-Reichenhall. Apart from being less ambiguous, this is a defacto standard on web-forms.


edit2: As a note about Navid Rahmani answer, because it opens up the potential for really severe database corruption, costs overtime for sys-admins, the programmers, who'll have no starting point and clue for why their application fails, and tons of money. That is, if this creepy corruption is ever discovered before your client has already lost its clients.

Code in question

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
    string[] strings = ((string)value).Split(' ');
    return strings;
}

My question was how it would handle multipart names. He answered one could use "," then. My reply:

Your code does not handle this, as it does not handle the case with less than two name components (you'll get an IndexOutOfRangeException). Additionally, your code does not handle multiple or forgotten whitespaces. For each additional whitespace, it produces a seperate value-token, potentially ruining database entries with dissapearing data (because you only read the first two entries). Further, you must validate if the user did not forget the comma or mistyped e.g. with a semicolon or slash. All these problems cease to exist with seperation into distinct fields. User input is the number one security and program stability dread.

<TextBlock Name="textBox">
  <TextBlock.Text>
    <MultiBinding>
      <Binding Path="FirstName"/>
      <Binding Path="LastName"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

Something like this should get you close. This was not written in VS so I'm not sure if it's syntactically correct.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top