简体   繁体   中英

WPF Tookit - IntegerUpDown

How do I get the value from the IntergerUpDown control? I'am using this : http://wpftoolkit.codeplex.com/wikipage?title=NumericUpDown Here is the code from MainWindow.xaml

 <extToolkit:IntegerUpDown Value="{Binding Mode=TwoWay, Path=CurrentCount}" HorizontalAlignment="Left" Margin="119,111,0,148" Increment="1" Maximum="10" Minimum="1" Width="100"/>

Here is the code from MainWindow.xaml.cs

 public int CurrentCount { get; set; }
 private void button1_Click(object sender, RoutedEventArgs e)
    {
        CurrentCount = 10;
         int len = Talker.BlahBlahBlah(textBox1.Text, CurrentCount);
    }

So basically I want to pass the value chosen by the user as an int into the method BlahBlahBlah. Do I need to create a View Model and bind it to a CurrentValue property? Can you please provide me with sample code which gets the value from the UI?

So here is my Talker Class:

 class Talker
{
    public static int BlahBlahBlah(string thingToSay, int numberOfTimes)
    {
        string finalString = "";
        for (int i = 0; i < numberOfTimes; i++)
        {
            finalString = finalString + thingToSay + "\n";
        }

        MessageBox.Show(finalString);
        return finalString.Length;
    }
}

This is the method inside ViewModelBase:

    public virtual int CurrentCount
    {
        get { return _CurrentCount; }
        set
        {
            _CurrentCount = value;
            OnPropertyChanged("CurrentCount");
        }
    }

Question is how do I link it together??

Peace

Andrew

The proper, MVVM-correct way of doing this is to bind the Value to a property on your ViewModel.

public int CurrentCount
{
    get { return _CurrentCount; }
    set
    {
        _CurrentCount = value;

    }
}

I recommend Josh Smith's excellent article on MVVM.

One thing you might want to do later is update the CurrentCount from code and have it reflect correctly in the IntegerUpDown control. To do this, you'll have to inherit from the INotifyPropertyChanged interface. You can use his ViewModelBase class which does just that. Then, your ViewModel inherits from ViewModelBase and can call OnPropertyChanged to send property changed notifications:

public int CurrentCount
{
    get { return _CurrentCount; }
    set
    {
        _CurrentCount = value;
        base.OnPropertyChanged("CurrentCount");            
    }
}

One way to do it would be to name the IntegerUpDown control

<extToolkit:IntegerUpDown 
    x:Name="CurrentCountUpDown" 
    HorizontalAlignment="Left" 
    Margin="119,111,0,148" 
    Increment="1" 
    Maximum="10" 
    Minimum="1" 
    Width="100"/>

And then just refer to that control by name in your event handler:

private void button1_Click(object sender, RoutedEventArgs e)
{
    int len = Talker.BlahBlahBlah(textBox1.Text, CurrentCountUpDown.Value);
}

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