[英]C# WPF Binding to variable and property in code behind
I got an integer in my viewmodel and a string in my view that I need to show combined. 我的视图模型中有一个整数,而我的视图中有一个需要显示组合的字符串。
This is the code in the view: 这是视图中的代码:
Binding buttonBinding = new Binding() {
Path = new PropertyPath(nameof(ButtonViewModel.MyInteger)),
};
_button.SetBinding(Button.ContentProperty, buttonBinding);
Can I attach my string at some point or do I need to transfer it into the viewmodel and make the property a single string? 我可以在某个时候附加我的字符串,还是需要将其转移到viewmodel中并使该属性成为单个字符串?
You could use the ContentStringFormat
property to specify a StringFormat
for the binding: 您可以使用ContentStringFormat
属性为绑定指定StringFormat
:
string s = "some string...";
Binding buttonBinding = new Binding()
{
Path = new PropertyPath(nameof(ButtonViewModel.MyInteger)),
};
_button.SetBinding(Button.ContentProperty, buttonBinding);
_button.ContentStringFormat = $"{{0}} {s}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.