将滑块绑定到文本框:
textbox.Text = Slider1.Value.ToString();
但是如何将文本框绑定到滑块?
答案 0 :(得分:0)
建立@Dmitry,尝试这样的事情:
private void textbox_TextChanged(object sender, EventArgs e)
{
try
{
// Convert the text to a Double and assign to slider
double value;
if (double.TryParse(textbox.Text, out value))
{
Slider1.Value = value;
// If the number is valid, display it in Black.
textBox.ForeColor = Color.Black;
}
else
{
// If the number is invalid, display it in Red.
textBox.ForeColor = Color.Red;
}
}
catch
{
// If there is an error, display the text using the system colors.
textBox.ForeColor = SystemColors.ControlText;
}
}
并且不要忘记将此事件代码连接到TextChanged事件。
textbox.TextChanged += new System.EventHandler(this.textBox_TextChanged);
答案 1 :(得分:0)
如果您正在谈论WPF
,那么您可以在控制器上创建一个属性:
private int _Text;
public int Text
{
get
{
return this._Text;
}
set
{
if (this._Text != value)
{
this._Text = value;
this.OnPropertyChanged("Text");
}
}
}
然后像这样绑定Slider
和TextBox
:
<TextBox Text={Binding Text}" />
<Slider Value={Binding Text}" />
确保您的控制器是您的xaml.cs
文件中的DataContext ...设置它:
Controller = new MyController();
this.DataContext = Controller;