说明:
1、将textbox与变量Show进行绑定
测试代码:
Xaml
<Grid Background="White">
<TextBox x:Name="textBox" Text="{Binding Path=Show, Mode=TwoWay}" HorizontalAlignment="Left" Height="38" Margin="124,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="261"/>
<Slider Name="sli" TickFrequency="1" Maximum="100" ValueChanged="sli_ValueChanged"></Slider>
</Grid>
C#
public partial class ChannelCalibration : Page
{
public ChannelCalibration()
{
InitializeComponent();
// mtextshow.show = "asdasdas"; 可以这样写。但是必须是变量show而不是Show否则会报错
textBox.DataContext = mtextshow;//textBox为控件名
}
private void sli_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
mtextshow.Show = "abcd"+ Convert.ToString(sli.Value);//此代码表示Slider滑块移动触发ValueChanged事件,并将Value传递给Person.IntValue;
}
MyTextshow mtextshow = new MyTextshow();
}
class MyTextshow : INotifyPropertyChanged //绑定对象
{
public string show;//显示
public event PropertyChangedEventHandler PropertyChanged;
public string Show
{
get { return show; }
set
{
show = value;
PropertyChanged(this, new PropertyChangedEventArgs("Show"));
}
}
}
}
效果图:
非字符串测试代码:
Xaml:
<Grid Background="White">
<TextBox x:Name="textBox" Text="{Binding Path=ScaleFactor, Mode=TwoWay}" HorizontalAlignment="Left" Height="38" Margin="124,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="261"/>
<Slider Name="sli" TickFrequency="1" Maximum="100" ValueChanged="sli_ValueChanged"></Slider>
</Grid>
C#
namespace WpfApp1.Pages
{
/// <summary>
/// ChannelCalibration.xaml 的交互逻辑
/// </summary>
public partial class ChannelCalibration : Page
{
public ChannelCalibration()
{
InitializeComponent();
textBox.DataContext = calibration;//textBox为控件名
}
private void sli_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
calibration.ZeroFactor = Convert.ToString(sli.Value);
calibration.ScaleFactor = Convert.ToDouble(sli.Value);
}
Calibration calibration = new Calibration();
}
class Calibration : INotifyPropertyChanged //绑定对象
{
private String zeroFactor;//零点校准
private double scaleFactor; //比例系数
public event PropertyChangedEventHandler PropertyChanged;
public String ZeroFactor
{
get { return zeroFactor; }
set
{
zeroFactor = value;
PropertyChanged(this, new PropertyChangedEventArgs("ZeroFactor"));
}
}
public double ScaleFactor
{
get { return scaleFactor; }
set
{
scaleFactor = value;
PropertyChanged(this, new PropertyChangedEventArgs("ScaleFactor"));
}
}
}
}