将两个属性绑定到==>单控制

发布于 2024-11-15 08:35:47 字数 232 浏览 6 评论 0原文

假设我有属性

FIrstNameLastName

我需要将其与单个 textbox 绑定。

因此,在单个文本框中,我可以显示两者FirstNameLastName

那怎么可能呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏夜暖风 2024-11-22 08:35:47

您可以使用多重绑定来做到这一点

对于 OnWay 绑定,请使用此:

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

有关 MutiBinding 类的更多信息,请参阅 此处

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

挽清梦 2024-11-22 08:35:47

尝试 MultiBinding 类

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

Try the MultiBinding Class :

<TextBlock>
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource myConverter}">
      <Binding Path="FirstName" />
      <Binding Path="LastName" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>
辞取 2024-11-22 08:35:47

也许是第三个属性 FullName

为了获得更好的帮助,请提出更好的问题(例如,您的类型是什么)。

编辑:无论如何,我建议至少有两个文本框,这样你就可以安全地处理“多部分名称”,就像上面提到的Karl Heinz Schmidt-Meyer von Neuenhausen zu Bad-Reichenhall。除了不太含糊之外,这是网络表单事实上的标准。


edit2:作为关于 Navid Rahmani 答案的注释,因为它可能会导致真正严重的数据库损坏、系统管理员和程序员的加班成本,他们将没有起点和线索来了解应用程序失败的原因,以及大量的问题 钱。也就是说,如果在您的客户已经失去其客户之前发现这种令人毛骨悚然的腐败行为。

有问题的代码

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

我的问题是它将如何处理多部分名称。他回答说可以用“,”。我的回复:

您的代码无法处理此问题,因为它无法处理名称组件少于两个的情况(您将收到 IndexOutOfRangeException)。此外,您的代码不处理多个或被遗忘的空格。对于每个额外的空格,它都会生成一个单独的值标记,可能会随着数据的消失而破坏数据库条目(因为您只读取前两个条目)。此外,您必须验证是否用户没有忘记逗号或输入错误,例如使用分号或斜杠。当分成不同的领域时,所有这些问题都不再存在。用户输入是最令人担忧的安全性和程序稳定性。

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.

三生殊途 2024-11-22 08:35:47
<TextBlock Name="textBox">
  <TextBlock.Text>
    <MultiBinding>
      <Binding Path="FirstName"/>
      <Binding Path="LastName"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

像这样的事情应该会让你接近。这不是用 VS 编写的,所以我不确定它的语法是否正确。

<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文