如何使用DependencyProperty设置TextBox的绑定

时间:2014-10-02 13:26:20

标签: c# wpf xaml data-binding textbox

我有一个带有DataGrid和TextBox的自定义UserControl,我试图使用DependencyProperties将这些元素数据绑定到这些元素。绑定适用于DataGrid,但不适用于TextBox。

代码:

public static readonly DependencyProperty BuiDataProperty = DependencyProperty.Register("BuiData", typeof(IEnumerable), typeof(BelastingTab), new PropertyMetadata(default(IEnumerable), BuiDataChanged));

private static void BuiDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var Object = d as BelastingTab;
    if (Object == null) return;
    Object.BuiDataDataSourceChanged(d, e);
}

private void BuiDataDataSourceChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
    BuiDataTabel.ItemsSource = dependencyPropertyChangedEventArgs.NewValue as IEnumerable;
}

public IEnumerable BuiData
{
    get { return (IEnumerable)GetValue(BuiDataProperty); }
    set { SetValue(BuiDataProperty, value); }
}

在主XAML中:

<src:BelastingTab BuiData="{Binding Path=Static.BuienRegulier[0].BuiTabel}"/>

这是设置DataGrid绑定的代码,我将如何为TextBox做同样的事情?

编辑: 这就是我目前所拥有的,

主要XAML:

<src:BelastingTab BuiData="{Binding Path=Static.BuienRegulier[0].BuiTabel}" HerhalingsTijd="{Binding Path=Static.BuienRegulier[0].HerhalingsTijd}"/>

这是指一个字符串。在UserControl XAML中:

<TextBox Text="{Binding HerhalingsTijd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

在UserControl XAML CS中:

public static readonly DependencyProperty HerhalingsTijdProperty = DependencyProperty.Register("HerhalingsTijd", typeof(string), typeof(BelastingTab), new PropertyMetadata(string.Empty));

public string HerhalingsTijd
{
    get { return (string)GetValue(HerhalingsTijdProperty); }
    set { SetValue(HerhalingsTijdProperty, value); }
}

1 个答案:

答案 0 :(得分:2)

我认为做你想做的事没有问题。我创建了一个简单的测试应用程我将在此提供代码,希望它能帮助您以某种方式解决您的错误。

UserControl1代码:

public partial class UserControl1 : UserControl
{
    public static DependencyProperty TxtBoxValueProperty = DependencyProperty.Register("TxtBoxValue", typeof(String), typeof(UserControl1));

    public String TxtBoxValue
    {
        get { return (String)GetValue(TxtBoxValueProperty); }
        set 
        {
            SetValue(TxtBoxValueProperty, value);
        }
    }

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
        if (e.Property == TxtBoxValueProperty)
        {
            // Do whatever you want with it
        }
    }

    public UserControl1()
    {
        InitializeComponent();
    }
}

用户控制Xaml:

<StackPanel>
    <TextBox Text="{Binding TxtBoxValue, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}, Mode=TwoWay}" Width="100" Height="50"/>
    <TextBox></TextBox>
</StackPanel>

主窗口xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:WpfApplication1"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <local:UserControl1 TxtBoxValue="{Binding TextBoxValue, Mode=TwoWay}"></local:UserControl1>
</Grid>

背后的主窗口代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    CancellationTokenSource cTS;
    CancellationToken cT;

    private String _textBoxValue;
    public String TextBoxValue
    {
        get { return _textBoxValue; }
        set 
        {
            _textBoxValue = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("TextBoxValue"));
            }

            if (_textBoxValue.Contains("enough"))
            {
                cTS.Cancel();
            }
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        cTS = new CancellationTokenSource();
        cT = cTS.Token;
        Task.Factory.StartNew(ChangeTextBoxValue, cT);
    }

    public void ChangeTextBoxValue()
    {
        while (true)
        {
            Random rnd = new Random(DateTime.Now.Millisecond);
            TextBoxValue = (rnd.NextDouble() * 1000.0).ToString();
            Thread.Sleep(10000);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

请注意,我写得非常快,并且我通常使用它(除了通知我放入ViewModelBase)。

如果这对你的案件不起作用,要么我不理解这个问题,要么你有一些非常具体的东西,但我对此表示怀疑。