简体   繁体   中英

TextBox text Binding

I have a LineSeries to which I am binding the data from an ObservableCollection of the type ChartData . Now, In my UI, I have a TextBox in which I need to show the Y value of the series. How do I bind the Value property to the TextBox

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ObservableCollection<ChartData> lineSeries1Data = new ObservableCollection<ChartData>();
        simChart.DataContext = lineSeries1Data;
    }
public class ChartData : INotifyPropertyChanged
{
    DateTime _Name;
    double _Value;

    public DateTime Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            OnPropertyChanged("Name");
        }
    }

    public double Value
    {
        get
        {
            return _Value;
        }
        set
        {
            _Value = value;
            OnPropertyChanged("Value");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }      
}

This is my XAML for the TextBox :

<TextBox Name="TxtSignal1Vh1" DataContext="lineSeries1Data" Text="{Binding ChartData.Value}" />

Here is some Working Code,Create a Usercontrol consisting of Chart+textboxes and then binds its Datacontext to respective observable collection.

 public MainWindow()
    {
        InitializeComponent();
        observableCollection<DataChart>1st=new observableCollection<DataChart>();
        observableCollection<DataChart>2nd=new observableCollection<DataChart>();
        win.DataContext = (1st Observable Collection)
        lose.DataContext=(2nd Observable Collection)
    }

MainWindows xaml

<Grid>
    <this:UserControl1 x:Name="win"  Margin="10,21,325,144"/>
    <this:UserControl1 x:Name="lose" Margin="275,21,10,144"/>
</Grid>

UserControl xaml

 <Grid>
    <TextBox  Text="{Binding Path=Value}" VerticalAlignment="Top" Width="164"/>
   <TextBox   Text="{Binding Path=Name}" VerticalAlignment="Top" Width="164"/>
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM