gpt4 book ai didi

c# - 使用多重绑定(bind)编辑文本框

转载 作者:行者123 更新时间:2023-12-03 10:35:20 26 4
gpt4 key购买 nike

我有一个使用多重绑定(bind)的文本框。用于此多重绑定(bind)的字段是来自我的数据 View 模型的属性

<TextBox>
<TextBox.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstProperty" />
<Binding Path="SecondProperty" />
</MultiBinding>
</TextBox.Text>
</TextBox>

我想在允许用户更新内容的同时保持这种行为;然后我会在我的数据 View 模型中检索文本框的内容。

是否可以?

最佳答案

虽然 pangabiMC 指向的链接使用我个人不会自己使用的转换器,但如果您将此行为放在 View 模型中,则调试和单元测试都会更容易。只需为您的文本框创建一个单独的属性 (CombinedProperty) 并使其与原始属性保持同步:

public class YourViewModel : ViewModelBase
{
private string _FirstProperty = "";
public string FirstProperty
{
get { return this._FirstProperty; }
set
{
this._FirstProperty = value;
RaisePropertyChanged(() => this.FirstProperty);
UpdateCombinedProperty();
}
}

private string _SecondProperty = "";
public string SecondProperty
{
get { return this._SecondProperty; }
set
{
this._SecondProperty = value;
RaisePropertyChanged(() => this.SecondProperty);
UpdateCombinedProperty();
}
}

private string _CombinedProperty = "";
public string CombinedProperty
{
get { return this._CombinedProperty; }
set
{
this._CombinedProperty = value;
RaisePropertyChanged(() => this.CombinedProperty);
UpdateSourceProperties();
}
}

private void UpdateCombinedProperty()
{
this.CombinedProperty = this.FirstProperty + " " + this.SecondProperty;
}

private void UpdateSourceProperties()
{
var fields = this.CombinedProperty.Split(' ');
if (fields.Length != 2)
return; // should handle validation properly
this.FirstProperty = fields[0];
this.SecondProperty = fields[1];
}

}

关于c# - 使用多重绑定(bind)编辑文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com