TextBox文本绑定

时间:2014-07-07 04:06:47

标签: c# wpf data-binding

我有一个LineSeries我绑定了ObservableCollection ChartData类型的数据。现在,在我的UI中,我有一个TextBox,我需要在其中显示该系列的Y值。如何将Value属性绑定到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));
        }
    }      
}

这是TextBox

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

1 个答案:

答案 0 :(得分:0)

这是一些工作代码,创建一个由图表+文本框组成的Usercontrol,然后将其Datacontext绑定到相应的可观察集合。

 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>