【问题标题】:How to set Binding of TextBox with DependencyProperty如何使用 DependencyProperty 设置 TextBox 的绑定
【发布时间】:2014-10-02 13:26:20
【问题描述】:

我有一个带有 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}"/>

这是指一个字符串。在用户控件 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); }
}

【问题讨论】:

    标签: c# wpf xaml data-binding textbox


    【解决方案1】:

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

    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)。

    如果这对你的情况不起作用,要么是我不明白这个问题,要么你有一些非常具体的问题,但我对此表示怀疑。

    【讨论】:

    • 我用我拥有的东西发布了一个编辑,但它不起作用
    • 添加RelativeSource 后它可以工作,你能解释一下这是做什么的吗?
    • 需要相对源,因为它是您可以从用户控件访问类属性背后的代码的唯一方法。您在控件中没有数据上下文,因此要么将数据上下文设置为 Self,要么使用相对源从后面的代码访问属性,因此绑定知道从何处获取属性。您需要进行一些实验和阅读才能完全理解这一点
    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2021-07-16
    • 2011-01-05
    • 2011-01-18
    • 1970-01-01
    相关资源
    最近更新 更多