dimanche 28 juin 2015

Can I Pass by Reference in XAML ICommand

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?

Aucun commentaire:

Enregistrer un commentaire