WPF 工具包 - IntegerUpDown
如何从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的、MVVM 正确的方法是将 Value 绑定到 ViewModel 上的属性。
我推荐 Josh Smith 关于 MVVM 的优秀文章。
您可能想要的一件事稍后要做的就是从代码中更新
CurrentCount
并使其在 IntegerUpDown 控件中正确反映。为此,您必须继承INotifyPropertyChanged
接口。您可以使用他的 ViewModelBase 类来完成此任务。然后,您的ViewModel
继承自ViewModelBase
并可以调用OnPropertyChanged
发送属性更改通知:The proper, MVVM-correct way of doing this is to bind the Value to a property on your ViewModel.
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 theINotifyPropertyChanged
interface. You can use hisViewModelBase
class which does just that. Then, yourViewModel
inherits fromViewModelBase
and can callOnPropertyChanged
to send property changed notifications:一种方法是命名
IntegerUpDown
控件,然后在事件处理程序中按名称引用该控件:
One way to do it would be to name the
IntegerUpDown
controlAnd then just refer to that control by name in your event handler: