WPF 工具包 - IntegerUpDown

发布于 2024-12-08 07:38:11 字数 1504 浏览 0 评论 0原文

如何从 IntergerUpDown 控件获取值? 我正在使用这个: http://wpftoolkit.codeplex.com/wikipage?title=NumericUpDown 这是 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"/>

这是 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);
    }

所以基本上我想将用户选择的值作为 int 传递到方法 BlahBlahBlah 中。我是否需要创建视图模型并将其绑定到 CurrentValue 属性?您能给我提供从 UI 获取值的示例代码吗?

所以这是我的 Talker 类:

 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;
    }
}

这是 ViewModelBase 内部的方法:

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

问题是如何将它链接在一起?

和平

安德鲁

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最单纯的乌龟 2024-12-15 07:38:11

正确的、MVVM 正确的方法是将 Value 绑定到 ViewModel 上的属性。

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

    }
}

我推荐 Josh Smith 关于 MVVM 的优秀文章。

您可能想要的一件事稍后要做的就是从代码中更新 CurrentCount 并使其在 IntegerUpDown 控件中正确反映。为此,您必须继承 INotifyPropertyChanged 接口。您可以使用他的 ViewModelBase 类来完成此任务。然后,您的 ViewModel 继承自 ViewModelBase 并可以调用 OnPropertyChanged 发送属性更改通知:

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

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");            
    }
}
此生挚爱伱 2024-12-15 07:38:11

一种方法是命名 IntegerUpDown 控件

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

,然后在事件处理程序中按名称引用该控件:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文