1

In this code:

        <ListBox 
            x:Name="DataList1"
            xmlns:local="clr-namespace:xaml_binding_commands"
            >
            <ListBox.Resources>
                <local:CommandUp x:Key="CommandUp1"></local:CommandUp>
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                    <TextBox
                        Text="{Binding Height,Mode=TwoWay,StringFormat=00\{0:d\}}"
                        InputScope="Number"/>

                    <RepeatButton
                        Content="+"
                        Command="{StaticResource CommandUp1}"
                        CommandParameter="{Binding }"
                        />

                    <TextBox
                        Text="{Binding Weight,Mode=TwoWay}"
                        InputScope="Number"/>

                    <RepeatButton
                        Content="+"
                        Command="{StaticResource CommandUp1}"
                        CommandParameter="{Binding Weight}"
                        />

                    </StackPanel>

                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>

And this

namespace xaml_binding_commands
{
    public class CommandUp : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        if (parameter is Entry)
        {
            Entry EntryToUp = (Entry)parameter;
            EntryToUp.Height +=1; // no worky, which field to increment?!
        }
        if (parameter is Int32)
        {
            Int32 EntryFieldToUp = (Int32)parameter;
            EntryFieldToUp += 1; // no worky
        }
    }
}

public class Entry : INotifyPropertyChanged
{

    private Int32 _Height;

    public Int32 Height
    {
        get { return _Height; }
        set { _Height = value; PropChange("Height"); }
    }

    private Int32 _Weight;

    public Int32 Weight
    {
        get { return _Weight; }
        set { _Weight = value; PropChange("Weight"); }
    }

    private void PropChange(String PropName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(PropName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

}



public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        this.Loaded += MainPage_Loaded;
    }

    private ObservableCollection<Entry> _People = new ObservableCollection<Entry>();

    public ObservableCollection<Entry> People
    {
        get { return _People; }
        set { _People = value; }
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        DataList1.ItemsSource = People;
        People.Add( new Entry() { Height=67, Weight=118 } );

    }
}

}

Can I pass the field that the textbox is bound to by reference? If I pass the entire class, an Entry, to the CommandParameter so it can operate and increment, I have no way of knowing which set of TextBoxes and Buttons caused the Command.Execute. If I pass the same thing that the TextBox is bound to, namely, individually: Weight,Height then the Command.Execute has no way of affecting the input. Usually I would PassByReference and my Int32 would be boxed, and my general operate function could operate on the by-ref parameter. Can I do this somehow in XAML?

Andyz Smith
  • 698
  • 5
  • 20
  • You can use the setters of the bound properties. Optionally with UpdateSrcTrigger=PropertyChanged. – H H Jun 28 '15 at 15:12
  • @HenkHolterman Use the setters to do what? I am already using the setters in an Notify scenario and that part works fine. The problem is in the ICommand. I'm not clear how UpdateSourceTrigger will affect my problem with ICommand either. – Andyz Smith Jun 28 '15 at 15:44

1 Answers1

1

If I was doing this, I would use MVVM Light and RelayCommand<string>. This means that you can pass in one (or more) parameters in as part of the binding to the ICommand.

This means that you could have multiple bindings to a single event handler attached to a button, and each binding could have a different parameter that let you know where it came from.

Update 1

MVVM Light is an MVVM library that is compatible with pretty much everything, from standard WPF to Windows 8.1 to Windows phone. See http://www.mvvmlight.net/. It is probably the most popular MVVM lib according to NuGet download stats, and the one that I tend to prefer.

For an example of how to use MVVM light with a CommandParameter, see the top voted answer at MVVM Light RelayCommand Parameters.

For an example of how to pass in two or more parameters to a RelayCommand, see How to Passing multiple parameters RelayCommand?

Update 2

Just looking at your code, I would use MVVM. I generally prefer MVVM to code behind (this is a whole discussion in itself). If you put all of your data in the ViewModel, and used bindings to let your XAML View update the ViewModel, I think things would become a lot easier to develop and maintain.

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305
  • Give me some context here: MultiBinding is similar to RelayCommand? AND What generation of WPF XAML is MVVM Light compatible with? Is it a library? – Andyz Smith Jun 28 '15 at 17:46
  • 1
    Yes, this is the general direction. An alternative is Caliburn.Micro with Attached messages. – H H Jun 28 '15 at 18:34
  • @HenkHolterman Thanks, if my thoughts have your vote then they're likely to be on the right track. – Contango Jun 28 '15 at 18:59
  • So, to answer specifically my question, you probably can't do what I want *exactly*. So at a higher level, really what I'm trying to do is pass a couple of things at a time. That's fine - answer looks good in that regard. Can you elaborate on any *specific* differences in what I'm actually doing -- using XAML markup as a view...using Entry and People as model, and using ICommand and NotifyPropertyChanged as sorts of Controllers ..can you elaborate.on the differences, between that, and what you suggest "put all your data in the ViewModel","use bindings to let XAML View update the ViewModel" – Andyz Smith Jun 28 '15 at 19:05
  • The point is do I need to start using some official 'Framework Toolkit' when I'm actually already following the design pattern expressed therein, and getting all the benefits of that. All benefits except maybe some extra bolt on ability to do MultipleRelayattached, blah. – Andyz Smith Jun 28 '15 at 19:08