简体   繁体   中英

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?

You could use the ContentStringFormat property to specify a StringFormat for the binding:

string s = "some string...";

Binding buttonBinding = new Binding()
{
    Path = new PropertyPath(nameof(ButtonViewModel.MyInteger)),
};
_button.SetBinding(Button.ContentProperty, buttonBinding);
_button.ContentStringFormat = $"{{0}} {s}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM