C# 如何使用DependencyProperty设置文本框的绑定
我有一个带有DataGrid和TextBox的自定义UserControl,我正在尝试使用DependencyProperties将内容数据绑定到这些元素。绑定对于DataGrid很好,但对于TextBox则不行 代码: 在主要的XAML中:C# 如何使用DependencyProperty设置文本框的绑定,c#,wpf,xaml,data-binding,textbox,C#,Wpf,Xaml,Data Binding,Textbox,我有一个带有DataGrid和TextBox的自定义UserControl,我正在尝试使用DependencyProperties将内容数据绑定到这些元素。绑定对于DataGrid很好,但对于TextBox则不行 代码: 在主要的XAML中: <src:BelastingTab BuiData="{Binding Path=Static.BuienRegulier[0].BuiTabel}"/> 我认为做你想做的事没有问题。我创建了一个简单的测试应用程序。我将在这里提供代码,希望它
<src:BelastingTab BuiData="{Binding Path=Static.BuienRegulier[0].BuiTabel}"/>
我认为做你想做的事没有问题。我创建了一个简单的测试应用程序。我将在这里提供代码,希望它能以某种方式帮助您修复错误 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>
请注意,我写这篇文章的速度非常快,并且做了我通常使用它的方式(除了通知我放入了ViewModelBase)
如果这在你的情况下不起作用,可能是我不理解这个问题,或者你有一些非常具体的问题,但我对此表示怀疑。我发布了一个编辑,其中包含了我所拥有的内容,在添加了你的相对资源后,它就不起作用了,你能给我解释一下这是做什么的吗?相对源代码是必需的,因为这是从用户控件访问代码隐藏类属性的唯一方法。控件中没有数据上下文,因此要么将数据上下文设置为Self,要么使用相对源从代码隐藏中访问属性,以便绑定知道从何处获取属性。你需要一点实验和阅读才能完全理解这一点
<TextBox Text="{Binding HerhalingsTijd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
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); }
}
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();
}
}
<StackPanel>
<TextBox Text="{Binding TxtBoxValue, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}, Mode=TwoWay}" Width="100" Height="50"/>
<TextBox></TextBox>
</StackPanel>
<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;
}