Viewed   9.5k times

I have a WPF Microsoft Surface Application and I'm using MVVM-Pattern.

I have some buttons that are created in code behind and I would like to bind commands to them, but I only know how that works in the XAML

like this:

<Custom:SurfaceButton Command="{Binding SaveReservationCommandBinding, Mode=OneWay}"/> 

But I cannot do it like this because my buttons do not exist in the XAML, only in the code behind.

So how would a command binding like that works in code behind?

 Answers

4

Assuming that you have a named your SurfaceButton to "SurfaceButton1" and you have access to an instance of the command, you can use the following code:

SurfaceButton1.Command = SaveReservationCommand;
Tuesday, December 20, 2022
2

The way I always do it is to specify the arguments as a "name"/"value" pair e.g.

myprogram.exe -arg1 value1 -arg2 value2

This means that when you parse the command line you can put the argument/value pairs in a Dictionary with the argument as the key. Then your arg("SetTime") will become:

MessageBox.Show(dictionary["SetTime"]);

(Obviously you don't want the actual dictionary to be public.)

To get the arguments in the first place you can use:

string[] args = Environment.GetCommandLineArgs();

This will return all the arguments so you will need to parse the array in steps of two (after first checking that the length is a multiple of two + 1):

The first element of the array is the name of the executing program - MSDN Page - so your loop needs to start from one:

for (int index = 1; index < args.Length; index += 2)
{
     dictionary.Add(args[index], args[index+1]);
}

This loops in steps of two as you define each argument is a pair of values: the identifier and the actual value itself, e.g.

my.exe -arg1 value1 -arg2 value2

Then you can simply see if the argument is specified by seeing if the key -arg1 is in the dictionary and then read it's value:

string value;
if (dictionary.TryGetValue(arg, out value))
{
    // Do what ever with the value
}

This means you can have the arguments in any order and omit any arguments you don't want to specify.

The only drawback with this method is if you have a flag like -debug (for example) which could be logically implemented with the presence or absence of the flag will need to be specified as -debug true (or 1 or on), but it does simplify things if you have flags that do require values (like configuration file paths, database connection strings etc.)

Wednesday, October 5, 2022
1

The BindingOperations class enables you to "force" the binding updates in either direction.

Let's say there is a string property X in the code behind and there is a TextBox in XAML, bound to that property:

// C#:
public string X { get; set; }

// XAML:
<TextBox Name="textBox1" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}, Path=X}" />

To copy from textBox1.Text to X do the following:

BindingOperations.GetBindingExpression(textBox1, TextBox.TextProperty).UpdateSource();

To copy from X to textBox1.Text do the following:

BindingOperations.GetBindingExpression(textBox1, TextBox.TextProperty).UpdateTarget();
Monday, October 3, 2022
 
panos_k
 
3

I think this should do it:

BindingExpression be = BindingOperations.GetBindingExpression((FrameworkElement)yourComboBox, ((DependencyProperty)Button.SelectedItemProperty));
string Name = be.ParentBinding.Path.Path;

To give credit where it's due.

Sunday, August 28, 2022
 
riva
 
5

I do not know your real intent, but the code below should help you.

You can try this by adding some arguments on project debug settings.

If your parameters contains blank spaces you should use "" signs to distinguish them. But instead of doing this you can use Application Config file for some cases. You can add some Settings from Project Settings or directly editing by settings.settings file.

If you need/fancy startup args, here is it.

//App
public partial class App : Application
{
    public string CustomerCode { get; set; }
    protected override void OnStartup(StartupEventArgs e)
    {

        this.CustomerCode=e.Args[0].ToString();
        base.OnStartup(e);
    }
}


//MainWindow
 public partial class MainWindow : Window
{
    public MainWindow()
    {
        var hereIsMyCode=(Application.Current as App).CustomerCode;
        InitializeComponent();
    }
}
Tuesday, August 9, 2022
 
arrdem
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :