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"));
            }
        }
    }
}
发布了61 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xfeng20/article/details/91043404