WPF与C# 变量与标记绑定

说明:
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"));
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值